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.
5 #import "base/ios/ns_error_util.h"
7 #import <Foundation/Foundation.h>
9 #include "base/logging.h"
10 #include "base/mac/scoped_nsobject.h"
16 // Iterates through |error|'s underlying errors and returns them in an array.
17 NSArray* GetFullErrorChainForError(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];
29 NSError* GetFinalUnderlyingErrorFromError(NSError* error) {
31 return [GetFullErrorChainForError(error) lastObject];
34 NSError* ErrorWithAppendedUnderlyingError(NSError* original_error,
35 NSError* underlying_error) {
36 DCHECK(original_error);
37 DCHECK(underlying_error);
38 NSArray* error_chain = GetFullErrorChainForError(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