Chromium Code Reviews| Index: base/ios/nserror_util.mm |
| diff --git a/base/ios/nserror_util.mm b/base/ios/nserror_util.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..65ea358a540d036a04ee6496cd1799e1ed574e9c |
| --- /dev/null |
| +++ b/base/ios/nserror_util.mm |
| @@ -0,0 +1,50 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "base/ios/nserror_util.h" |
| + |
| +#import <Foundation/Foundation.h> |
|
stuartmorgan
2015/06/24 19:13:09
Blank line after this.
kkhorimoto
2015/06/24 20:01:42
Done.
|
| +#include "base/logging.h" |
| +#include "base/mac/scoped_nsobject.h" |
| + |
| +namespace base { |
| +namespace nserror_util { |
| + |
| +namespace { |
| +// Iterates through |error|'s underlying errors and returns them in an array. |
| +NSArray* GetUnderlyingErrorChainForError(NSError* error) { |
| + NSMutableArray* error_chain = [NSMutableArray array]; |
| + NSError* current_error = error; |
| + while (current_error) { |
| + [error_chain addObject:current_error]; |
| + current_error = current_error.userInfo[NSUnderlyingErrorKey]; |
| + } |
| + return error_chain; |
| +} |
| +} // namespace |
| + |
| +NSError* GetFinalUnderlyingErrorFromError(NSError* error) { |
| + DCHECK(error); |
| + return [GetUnderlyingErrorChainForError(error) lastObject]; |
| +} |
| + |
| +NSError* ErrorWithAppendedUnderlyingError(NSError* original_error, |
| + NSError* underlying_error) { |
| + DCHECK(original_error && underlying_error); |
|
stuartmorgan
2015/06/24 19:13:09
Separate the DCHECKs, so that if one fails it's im
kkhorimoto
2015/06/24 20:01:42
Done.
|
| + NSArray* error_chain = GetUnderlyingErrorChainForError(original_error); |
| + NSError* current_error = underlying_error; |
| + for (NSInteger idx = error_chain.count - 1; idx >= 0; --idx) { |
| + NSError* error = error_chain[idx]; |
| + scoped_nsobject<NSMutableDictionary> user_info( |
| + [error.userInfo mutableCopy]); |
| + [user_info setObject:current_error forKey:NSUnderlyingErrorKey]; |
| + current_error = [NSError errorWithDomain:error.domain |
| + code:error.code |
| + userInfo:user_info]; |
| + } |
| + return current_error; |
| +} |
| + |
| +} // namespace nserror_util |
| +} // namespace base |