Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "base/ios/nserror_util.h" | |
| 6 | |
| 7 #import <Foundation/Foundation.h> | |
|
stuartmorgan
2015/06/24 19:13:09
Blank line after this.
kkhorimoto
2015/06/24 20:01:42
Done.
| |
| 8 #include "base/logging.h" | |
| 9 #include "base/mac/scoped_nsobject.h" | |
| 10 | |
| 11 namespace base { | |
| 12 namespace nserror_util { | |
| 13 | |
| 14 namespace { | |
| 15 // Iterates through |error|'s underlying errors and returns them in an array. | |
| 16 NSArray* GetUnderlyingErrorChainForError(NSError* error) { | |
| 17 NSMutableArray* error_chain = [NSMutableArray array]; | |
| 18 NSError* current_error = error; | |
| 19 while (current_error) { | |
| 20 [error_chain addObject:current_error]; | |
| 21 current_error = current_error.userInfo[NSUnderlyingErrorKey]; | |
| 22 } | |
| 23 return error_chain; | |
| 24 } | |
| 25 } // namespace | |
| 26 | |
| 27 NSError* GetFinalUnderlyingErrorFromError(NSError* error) { | |
| 28 DCHECK(error); | |
| 29 return [GetUnderlyingErrorChainForError(error) lastObject]; | |
| 30 } | |
| 31 | |
| 32 NSError* ErrorWithAppendedUnderlyingError(NSError* original_error, | |
| 33 NSError* underlying_error) { | |
| 34 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.
| |
| 35 NSArray* error_chain = GetUnderlyingErrorChainForError(original_error); | |
| 36 NSError* current_error = underlying_error; | |
| 37 for (NSInteger idx = error_chain.count - 1; idx >= 0; --idx) { | |
| 38 NSError* error = error_chain[idx]; | |
| 39 scoped_nsobject<NSMutableDictionary> user_info( | |
| 40 [error.userInfo mutableCopy]); | |
| 41 [user_info setObject:current_error forKey:NSUnderlyingErrorKey]; | |
| 42 current_error = [NSError errorWithDomain:error.domain | |
| 43 code:error.code | |
| 44 userInfo:user_info]; | |
| 45 } | |
| 46 return current_error; | |
| 47 } | |
| 48 | |
| 49 } // namespace nserror_util | |
| 50 } // namespace base | |
| OLD | NEW |