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/ns_error_util.h" | |
| 6 | |
| 7 #import <Foundation/Foundation.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/mac/scoped_nsobject.h" | |
| 11 | |
| 12 namespace base { | |
| 13 namespace ios { | |
| 14 | |
| 15 namespace { | |
| 16 // Iterates through |error|'s underlying errors and returns them in an array. | |
|
stuartmorgan
2015/06/24 21:30:48
Since this actually returns |error| as well, the n
kkhorimoto
2015/06/24 22:17:59
Done.
| |
| 17 NSArray* GetUnderlyingErrorChainForError(NSError* error) { | |
| 18 NSMutableArray* error_chain = [NSMutableArray array]; | |
| 19 NSError* current_error = error; | |
| 20 while (current_error) { | |
| 21 DCHECK([current_error isKindOfClass:[NSError class]]); | |
| 22 [error_chain addObject:current_error]; | |
| 23 current_error = current_error.userInfo[NSUnderlyingErrorKey]; | |
| 24 } | |
| 25 return error_chain; | |
| 26 } | |
| 27 } // namespace | |
| 28 | |
| 29 NSError* GetFinalUnderlyingErrorFromError(NSError* error) { | |
| 30 DCHECK(error); | |
| 31 return [GetUnderlyingErrorChainForError(error) lastObject]; | |
| 32 } | |
| 33 | |
| 34 NSError* ErrorWithAppendedUnderlyingError(NSError* original_error, | |
| 35 NSError* underlying_error) { | |
| 36 DCHECK(original_error); | |
| 37 DCHECK(underlying_error); | |
| 38 NSArray* error_chain = GetUnderlyingErrorChainForError(original_error); | |
| 39 NSError* current_error = underlying_error; | |
| 40 for (NSInteger idx = error_chain.count - 1; idx >= 0; --idx) { | |
| 41 NSError* error = error_chain[idx]; | |
| 42 scoped_nsobject<NSMutableDictionary> user_info( | |
| 43 [error.userInfo mutableCopy]); | |
| 44 [user_info setObject:current_error forKey:NSUnderlyingErrorKey]; | |
| 45 current_error = [NSError errorWithDomain:error.domain | |
| 46 code:error.code | |
| 47 userInfo:user_info]; | |
| 48 } | |
| 49 return current_error; | |
| 50 } | |
| 51 | |
| 52 } // namespace ios | |
| 53 } // namespace base | |
| OLD | NEW |