forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSError+SSYAdds.m
executable file
·760 lines (642 loc) · 22 KB
/
NSError+SSYAdds.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#import "NSError+SSYAdds.h"
#import "NSString+Truncate.h"
#import "NSObject+DeepCopy.h"
#import "NSString+LocalizeSSY.h"
#import "NSDictionary+SimpleMutations.h"
#import "NSObject+MoreDescriptions.h"
#import "NSString+MorePaths.h"
#import "SSYDebug.h"
NSString* const SSYMethodNameErrorKey = @"Method Name" ;
NSString* const SSYLocalizedTitleErrorKey = @"Localized Title" ;
NSString* const SSYUnderlyingExceptionErrorKey = @"Underlying Exception" ;
NSString* const SSYTimestampErrorKey = @"Timestamp" ;
NSString* const SSYRecoveryAttempterUrlErrorKey = @"RecoveryAttempterUrl" ;
NSString* const SSYRetryDateErrorKey = @"RetryDate" ;
NSString* const SSYRecoveryAttempterIsAppDelegateErrorKey = @"RecoveryAttempterIsAppDelegate" ;
NSString* const SSYHttpStatusCodeErrorKey = @"HTTP Status Code" ;
NSString* const SSYDontShowSupportEmailButtonErrorKey = @"dontShowSupportEmailButton" ;
NSString* const SSYIsOnlyInformationalErrorKey = @"isOnlyInformational" ;
NSString* const SSYIsLoggedErrorKey = @"isLogged" ;
NSString* const SSYDidRecoverInvocationErrorKey = @"didRecoverInvocation" ;
NSString* const SSYHelpAnchorErrorKey = @"helpAnchor" ;
NSString* const SSYDidTruncateErrorDescriptionTrailer = @"\n\n*** Note: That error description was truncated! ***" ;
@implementation NSError (SSYAdds)
+ (NSString*)myDomain {
NSString* domain = [[NSBundle mainBundle] bundleIdentifier] ;
// Background/daemon/helper/tools will usually not have a bundle...
if (!domain) {
NSString* path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0] ;
domain = [path lastPathComponent] ;
}
if (!domain) {
domain = @"UnknownErrorDomain" ;
}
return domain ;
}
+ (NSError*)errorWithLocalizedDescription:(NSString*)localizedDescription
code:(int)code
prettyFunction:(const char*)prettyFunction {
NSDictionary* userInfo = nil ;
if (localizedDescription) {
NSString* const CFBundleVersionKey = @"CFBundleVersion" ;
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
localizedDescription, NSLocalizedDescriptionKey,
[[NSBundle mainBundle] objectForInfoDictionaryKey:CFBundleVersionKey], CFBundleVersionKey,
// The following gets added in -[SSYAlert support:]. It would be nice to do it here instead.
// But then I'd have to #import SSYSystemDescriber into any project using this file.
//[SSYSystemDescriber softwareVersionAndArchitecture], @"System Description",
nil] ;
}
NSError* error = [NSError errorWithDomain:[self myDomain]
code:code
userInfo:userInfo] ;
error = [error errorByAddingPrettyFunction:prettyFunction] ;
error = [error errorByAddingTimestampNow] ;
return error ;
}
- (NSString*)uniqueKeyForBaseKey:(NSString*)baseKey
sequenceNumber:(NSInteger)sequenceNumber {
NSString* uniqueKey ;
if (sequenceNumber == 0) {
uniqueKey = baseKey ;
}
else {
uniqueKey = [NSString stringWithFormat:
@"%@-%02d",
[baseKey description],
sequenceNumber] ;
}
return uniqueKey ;
}
- (NSError*)errorByAddingUserInfoObject:(id)object
forKey:(NSString*)baseKey {
NSError* answer ;
if (object != nil) {
NSDictionary* userInfo = [self userInfo] ;
if (userInfo) {
// Find the next, unused sequence number
// Will be 0 if there are no such keys yet with this base
// Will be 1 if there is 1 other such key with this base
// etc.
NSInteger nextSequenceNumber = 0 ;
while ([userInfo objectForKey:[self uniqueKeyForBaseKey:baseKey
sequenceNumber:nextSequenceNumber]]) {
nextSequenceNumber++ ;
}
// Increment the sequence number of each existing key, if any
NSInteger i ;
for (i=nextSequenceNumber; i>0; i--) {
NSString* oldKey = [self uniqueKeyForBaseKey:baseKey
sequenceNumber:(i-1)] ;
NSString* newKey = [self uniqueKeyForBaseKey:baseKey
sequenceNumber:i] ;
userInfo = [userInfo dictionaryBySettingValue:[userInfo objectForKey:oldKey]
forKey:newKey] ;
}
// Set the newest object as the base key
userInfo = [userInfo dictionaryBySettingValue:object
forKey:baseKey] ;
}
else {
userInfo = [NSDictionary dictionaryWithObject:object
forKey:baseKey] ;
}
answer = [NSError errorWithDomain:[self domain]
code:[self code]
userInfo:userInfo] ;
}
else {
answer = self ;
}
return answer ;
}
- (NSError*)errorByOverwritingUserInfoObject:(id)object
forKey:(NSString*)key {
NSError* answer ;
if (object != nil) {
NSDictionary* userInfo = [self userInfo] ;
if (userInfo) {
userInfo = [userInfo dictionaryBySettingValue:object
forKey:key] ;
}
else {
userInfo = [NSDictionary dictionaryWithObject:object
forKey:key] ;
}
answer = [NSError errorWithDomain:[self domain]
code:[self code]
userInfo:userInfo] ;
}
else {
answer = self ;
}
return answer ;
}
- (NSError*)errorByAddingLocalizedDescription:(NSString*)newText {
return [self errorByAddingUserInfoObject:newText
forKey:NSLocalizedDescriptionKey] ;
}
- (NSError*)errorByAddingLocalizedFailureReason:(NSString*)failureReason {
NSError* answer ;
if (failureReason != nil) {
NSDictionary* userInfo = [self userInfo] ;
if (userInfo) {
userInfo = [userInfo dictionaryBySettingValue:failureReason
forKey:NSLocalizedFailureReasonErrorKey] ;
}
else {
userInfo = [NSDictionary dictionaryWithObject:failureReason
forKey:NSLocalizedFailureReasonErrorKey] ;
}
answer = [NSError errorWithDomain:[self domain]
code:[self code]
userInfo:userInfo] ;
}
else {
answer = self ;
}
return answer ;
}
- (NSError*)errorByAppendingLocalizedFailureReason:(NSString*)newFailureReason {
NSError* answer ;
if (newFailureReason) {
NSString* oldFailureReason = [self localizedFailureReason] ;
if (oldFailureReason) {
newFailureReason = [NSString stringWithFormat:
@"%@ %@",
oldFailureReason,
newFailureReason] ;
}
answer = [self errorByAddingLocalizedFailureReason:newFailureReason] ;
}
else {
answer = self ;
}
return answer ;
}
- (NSError*)errorByAddingPrettyFunction:(const char*)prettyFunction {
NSError* error ;
if (prettyFunction != NULL) {
error = [self errorByAddingUserInfoObject:[NSString stringWithCString:prettyFunction
encoding:NSUTF8StringEncoding]
forKey:SSYMethodNameErrorKey] ;
}
else {
error = self ;
}
return error ;
}
- (NSError*)errorByAddingLocalizedRecoverySuggestion:(NSString*)newText {
return [self errorByAddingUserInfoObject:newText
forKey:NSLocalizedRecoverySuggestionErrorKey] ;
}
- (NSError*)errorByAddingRecoveryAttempter:(id)recoveryAttempter {
return [self errorByAddingUserInfoObject:recoveryAttempter
forKey:NSRecoveryAttempterErrorKey] ;
}
- (NSError*)errorByAddingRecoveryAttempterUrl:(NSURL*)url {
return [self errorByAddingUserInfoObject:url
forKey:SSYRecoveryAttempterUrlErrorKey] ;
}
- (NSError*)errorByAddingRecoveryAttempterIsAppDelegate {
return [self errorByAddingUserInfoObject:[NSNumber numberWithBool:YES]
forKey:SSYRecoveryAttempterIsAppDelegateErrorKey] ;
}
- (NSError*)errorByAddingLocalizedRecoveryOptions:(NSArray*)recoveryOptions {
return [self errorByAddingUserInfoObject:recoveryOptions
forKey:NSLocalizedRecoveryOptionsErrorKey] ;
}
- (NSError*)errorByAddingDidRecoverInvocation:(NSInvocation*)didRecoverInvocation {
return [self errorByAddingUserInfoObject:didRecoverInvocation
forKey:SSYDidRecoverInvocationErrorKey] ;
}
- (NSInvocation*)didRecoverInvocation {
return [[self userInfo] objectForKey:SSYDidRecoverInvocationErrorKey] ;
}
- (NSError*)errorByAddingHelpAnchor:(NSString*)helpAnchor {
return [self errorByAddingUserInfoObject:helpAnchor
forKey:SSYHelpAnchorErrorKey] ;
}
- (NSString*)helpAnchor {
return [[self userInfo] objectForKey:SSYHelpAnchorErrorKey] ;
}
- (NSError*)underlyingError {
return [[self userInfo] objectForKey:NSUnderlyingErrorKey] ;
}
- (NSError*)bottomError {
NSError* bottomError ;
NSError* nextError = self ;
while (nextError) {
bottomError = nextError ;
nextError = [nextError underlyingError] ;
}
return bottomError ;
}
- (NSError*)errorByAddingUnderlyingError:(NSError*)underlyingError {
return [[self bottomError] errorByAddingUserInfoObject:underlyingError
forKey:NSUnderlyingErrorKey] ;
}
- (NSError*)errorByAddingUnderlyingException:(NSException*)exception {
NSMutableDictionary* exceptionInfo = [[NSMutableDictionary alloc] init] ;
id value ;
value = [exception name] ;
if (value) {
[exceptionInfo setObject:value
forKey:@"Name"] ;
}
value = [exception reason] ;
if (value) {
[exceptionInfo setObject:value
forKey:@"Reason"] ;
}
value = [exception userInfo] ;
if (value) {
[exceptionInfo setObject:value
forKey:@"User Info"] ;
}
NSDictionary* info = [NSDictionary dictionaryWithDictionary:exceptionInfo] ;
[exceptionInfo release] ;
return [self errorByAddingUserInfoObject:info
forKey:SSYUnderlyingExceptionErrorKey] ;
}
- (NSError*)errorByAddingLocalizedTitle:(NSString*)title {
return [self errorByAddingUserInfoObject:title
forKey:SSYLocalizedTitleErrorKey] ;
}
- (NSError*)errorByRemovingRecoveryAttempter {
NSMutableDictionary* userInfo = [[self userInfo] mutableCopy] ;
[userInfo removeObjectForKey:NSRecoveryAttempterErrorKey] ;
NSError* error = [NSError errorWithDomain:[self domain]
code:[self code]
userInfo:userInfo] ;
[userInfo release] ;
return error ;
}
- (NSError*)errorByAddingTimestamp:(NSDate*)date {
return [self errorByAddingUserInfoObject:date
forKey:SSYTimestampErrorKey] ;
}
- (NSError*)errorByAddingTimestampNow {
return [self errorByAddingTimestamp:[NSDate date]] ;
}
- (NSDate*)timestamp {
return [[self userInfo] objectForKey:SSYTimestampErrorKey] ;
}
- (NSError*)errorByAddingDontShowSupportEmailButton {
return [self errorByAddingUserInfoObject:[NSNumber numberWithBool:YES]
forKey:SSYDontShowSupportEmailButtonErrorKey] ;
}
- (BOOL)shouldShowSupportEmailButton {
return (![[self userInfo] objectForKey:SSYDontShowSupportEmailButtonErrorKey]) ;
}
- (NSError*)errorByAddingIsOnlyInformational {
return [self errorByAddingUserInfoObject:[NSNumber numberWithBool:YES]
forKey:SSYIsOnlyInformationalErrorKey] ;
}
- (NSError*)maybeAddMountVolumeRecoverySuggestion {
NSError* error = self ;
NSString* path = [[self userInfo] objectForKey:@"Path"] ;
NSString* volumePath = [path volumePath] ;
if (volumePath) {
if (![[NSFileManager defaultManager] fileExistsAtPath:volumePath]) {
NSString* msg = [NSString stringWithFormat:
@"Mount the volume '%@'",
[[volumePath pathComponents] objectAtIndex:2]] ;
error = [self errorByAddingLocalizedRecoverySuggestion:msg] ;
}
}
return error ;
}
- (NSError*)errorByAddingBacktrace {
return [self errorByAddingUserInfoObject:SSYDebugBacktrace()
forKey:@"Backtrace"] ;
}
- (BOOL)isOnlyInformational {
return ([[self userInfo] objectForKey:SSYIsOnlyInformationalErrorKey] != nil) ;
}
- (NSError*)errorByAddingIsLogged {
return [self errorByAddingUserInfoObject:[NSNumber numberWithBool:YES]
forKey:SSYIsLoggedErrorKey] ;
}
- (BOOL)isLogged {
return ([[[self userInfo] objectForKey:SSYIsLoggedErrorKey] boolValue]) ;
}
- (NSString*)longDescriptionWithIndentationLevel:(NSInteger)indentationLevel
truncateForEmail:(BOOL)truncateForEmail {
// Unfortunately, if you carefully read the documentation for NSError you'll see
// that the NSLocalizedDescriptionKey may or may not be in the userInfo
// dictionary. So, in order to present a consistent email content, we first
// put it into the userInfo dictionary if it is not in there.
NSDictionary* userInfo = [self userInfo] ;
if (![userInfo objectForKey:NSLocalizedDescriptionKey]) {
NSString* localizedDescription = [self localizedDescription] ;
if (localizedDescription) {
NSMutableDictionary* userInfoMute = [NSMutableDictionary dictionaryWithDictionary:userInfo] ;
[userInfoMute setObject:localizedDescription
forKey:NSLocalizedDescriptionKey] ;
userInfo = userInfoMute ;
}
}
NSMutableString* indentation = [NSMutableString string] ;
int ii ;
for (ii=0; ii<indentationLevel; ii++) {
[indentation appendString:@" "] ;
}
// Set truncation limits
int userInfoMaxKeyLength ;
int userInfoMaxValueLength;
int userInfoMaxTotalLength;
if (truncateForEmail) {
userInfoMaxKeyLength = 256 ;
userInfoMaxValueLength = 16384 ;
userInfoMaxTotalLength = 32768;
}
else {
userInfoMaxKeyLength = 65536 ;
userInfoMaxValueLength = 524288 ;
userInfoMaxTotalLength = 2097152 ; // 2 MB
}
BOOL didTruncate = NO ;
// The following is to make sure that if something really went haywire and caused
// the NSError's userInfo dictionary to become very large, we truncate each key and
// values, and the aggregate description, to a readable length so that the user
// will not be afraid to send it and the support engineer will not be afraid to
// read it.
NSMutableString* truncatedUserInfo = [NSMutableString string] ;
// For readability, sort keys so that the underyling error, if any, is last.
NSMutableArray* keys = [[userInfo allKeys] mutableCopy] ;
if ([keys indexOfObject:NSUnderlyingErrorKey] != NSNotFound) {
[keys removeObject:NSUnderlyingErrorKey] ;
[keys addObject:NSUnderlyingErrorKey] ;
}
NSEnumerator* e = [keys objectEnumerator] ;
id key ;
id value ;
while ((key = [e nextObject]) != nil) {
if ([truncatedUserInfo length] > userInfoMaxTotalLength) {
didTruncate = YES ;
break ;
}
value = [userInfo objectForKey:key] ;
NSString* keyDescription = [key description] ;
NSString* valueDescription = nil ;
if ([value respondsToSelector:@selector(longDescriptionWithIndentationLevel:truncateForEmail:)]) {
NSInteger nextIndentationLevel = (indentationLevel + 1) ;
valueDescription = [value longDescriptionWithIndentationLevel:nextIndentationLevel
truncateForEmail:truncateForEmail] ;
}
if (!valueDescription) {
valueDescription = [value longDescription] ;
}
NSString* truncatedKey = [keyDescription stringByTruncatingMiddleToLength:userInfoMaxKeyLength
wholeWords:NO] ;
if ([truncatedKey length] < [keyDescription length]) {
didTruncate = YES ;
}
NSString* truncatedValue = [valueDescription stringByTruncatingMiddleToLength:userInfoMaxValueLength
wholeWords:NO] ;
if ([truncatedValue length] < [valueDescription length]) {
didTruncate = YES ;
}
[truncatedUserInfo appendFormat:
@"%@ **Key: %@\n"
@"%@ Value: %@\n",
indentation,
truncatedKey,
indentation,
truncatedValue] ;
}
[keys release] ;
NSString* maybeTruncateTrailer = didTruncate ? SSYDidTruncateErrorDescriptionTrailer : @"" ;
return [NSString stringWithFormat:
@"%@NSError %p\n"
@"%@*** code: %d\n"
@"%@*** domain: %@\n"
@"%@*** userInfo:\n%@%@",
indentation,
self,
indentation,
[self code],
indentation,
[self domain],
indentation,
truncatedUserInfo,
maybeTruncateTrailer] ;
}
- (void)appendIfExistsUserInfoValueForKey:(NSString*)key
withLabel:(NSString*)label
toDescription:(NSMutableString*)string {
NSString* more = [[self userInfo] objectForKey:key] ;
if (more) {
[string appendFormat:@"\n\n%@\n%@", label, more] ;
}
}
- (NSString*)descriptionForDialog {
NSMutableString* dialogDescription = [[self localizedDescription] mutableCopy] ;
[self appendIfExistsUserInfoValueForKey:NSLocalizedFailureReasonErrorKey
withLabel:[NSString localize:@"errorReasonLabel"]
toDescription:dialogDescription] ;
[self appendIfExistsUserInfoValueForKey:NSLocalizedRecoverySuggestionErrorKey
withLabel:[NSString localize:@"errorRecoveryLabel"]
toDescription:dialogDescription] ;
if ([NSError respondsToSelector:@selector(additionalKeysInDescriptionForDialog)]) {
NSArray* additionalKeys = [NSError performSelector:@selector(additionalKeysInDescriptionForDialog)] ;
NSEnumerator* e = [additionalKeys objectEnumerator] ;
NSString* key ;
while ((key = [e nextObject])) {
[self appendIfExistsUserInfoValueForKey:key
withLabel:[NSString localizeWeakly:key]
toDescription:dialogDescription] ;
}
}
NSString* answer = [dialogDescription copy] ;
[dialogDescription release] ;
return [answer autorelease] ;
}
- (NSString*)localizedTitle {
NSString* title = [[self userInfo] objectForKey:SSYLocalizedTitleErrorKey] ;
return title ;
}
- (NSString*)localizedDeepDescription {
BOOL moreThanOne = NO ;
NSMutableString* deepDescription = [[NSMutableString alloc] init] ;
NSError* underlyingError = self ;
while (underlyingError) {
if (moreThanOne) {
[deepDescription appendFormat:@"\n\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 %@ \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\n",
[NSString localize:@"causeUnderlying"]] ;
}
else {
moreThanOne = YES ;
}
NSString* nextPart ;
if ([underlyingError respondsToSelector:@selector(descriptionForDialog)]) {
nextPart = [underlyingError descriptionForDialog] ;
}
else {
nextPart = [underlyingError description] ;
}
// Some defensive programming, in case a weird error returns nil for either
// of the above...
if (nextPart) {
[deepDescription appendString:nextPart] ;
}
if ([underlyingError respondsToSelector:@selector(underlyingError)]) {
underlyingError = [underlyingError underlyingError] ;
}
else {
underlyingError = nil ;
}
}
NSString* answer = [deepDescription copy] ;
[deepDescription release] ;
return [answer autorelease] ;
}
- (NSString*)longDescription {
return [self longDescriptionWithIndentationLevel:0
truncateForEmail:NO] ;
}
- (NSString*)mailableLongDescription {
return [self longDescriptionWithIndentationLevel:0
truncateForEmail:YES] ;
}
- (NSData*)keyedArchive {
NSInteger code = [self code] ;
NSString* domain = [self domain] ;
NSDictionary* userInfo = [self userInfo] ;
NSDictionary* encodeableUserInfo = [userInfo mutableDeepCopyStyle:SSYDeepCopyStyleBitmaskEncodeable] ;
NSError* encodeableError = [NSError errorWithDomain:domain
code:code
userInfo:encodeableUserInfo] ;
[encodeableUserInfo release] ;
NSData* archive = [NSKeyedArchiver archivedDataWithRootObject:encodeableError] ;
return archive ;
}
- (NSInteger)mostUnderlyingErrorCode {
NSInteger code ;
NSError* error = self ;
while (error) {
code = [error code] ;
error = [error underlyingError] ;
}
return code ;
}
- (NSArray*)codesArray {
NSMutableArray* codes = [NSMutableArray array] ;
NSError* error = self ;
while (error) {
[codes addObject:[NSNumber numberWithInteger:[error code]]] ;
error = [error underlyingError] ;
}
return codes ;
}
- (NSSet*)allInvolvedCodesInDomain:(NSString*)domain {
NSMutableSet* codes = [NSMutableSet set] ;
NSError* error = self ;
while (error) {
NSString* errorDomain = [error domain] ;
if (errorDomain) {
if (!domain || [domain isEqualToString:errorDomain]) {
[codes addObject:[NSNumber numberWithInteger:[error code]]] ;
}
}
error = [error underlyingError] ;
}
NSSet* answer = [codes copy] ;
return [answer autorelease] ;
}
- (BOOL)involvesOneOfCodesInSet:(NSSet*)targetCodes
domain:(NSString*)domain {
for (NSNumber* targetCode in targetCodes) {
if ([[self allInvolvedCodesInDomain:domain] member:targetCode]) {
return YES ;
}
}
return NO ;
}
- (BOOL)involvesOneOfCodesInSet:(NSSet*)targetCodes {
return [self involvesOneOfCodesInSet:targetCodes
domain:nil] ;
}
- (BOOL)involvesOneOfMyCodesInSet:(NSSet*)targetCodes {
return [self involvesOneOfCodesInSet:targetCodes
domain:[NSError myDomain]] ;
}
- (BOOL)involvesCode:(NSInteger)code
domain:(NSString*)domain {
return [self involvesOneOfCodesInSet:[NSSet setWithObject:[NSNumber numberWithInteger:code]]
domain:domain] ;
}
- (BOOL)involvesCode:(NSInteger)code {
return [self involvesCode:code
domain:nil] ;
}
- (BOOL)involvesMyDomainAndCode:(NSInteger)code {
return [self involvesCode:code
domain:[NSError myDomain]] ;
}
- (BOOL)isNotFileNotFoundError {
if ([self code] != NSFileReadNoSuchFileError) {
return YES ;
}
if (![[self domain] isEqualToString:NSCocoaErrorDomain]) {
return YES ;
}
// Error is NSFileReadNoSuchFileError in NSCocoaErrorDomain.
return NO ;
}
- (BOOL)isRecoverable {
return (
(NO
|| [self recoveryAttempter]
|| [[self userInfo] objectForKey:SSYRecoveryAttempterUrlErrorKey]
|| [[[self userInfo] objectForKey:SSYRecoveryAttempterIsAppDelegateErrorKey] boolValue]
)
&&
([[self localizedRecoveryOptions] count] > 0)
) ;
}
- (NSError*)deepestRecoverableError {
NSMutableArray* underlyingErrors = [[NSMutableArray alloc] init] ;
[underlyingErrors addObject:self] ;
NSError* error = self ;
while ((error = [error underlyingError])) {
[underlyingErrors addObject:error] ;
}
NSEnumerator* bottomUpErrors = [underlyingErrors reverseObjectEnumerator] ;
[underlyingErrors release] ;
NSError* answer = nil ;
for (error in bottomUpErrors) {
if ([error isRecoverable]) {
answer = error ;
break ;
}
}
return answer ;
}
- (id)openRecoveryAttempterForRecoveryOption:(NSInteger)recoveryOption
error_p:(NSError**)error_p {
if (error_p) {
*error_p = nil ;
}
id recoveryAttempter = [self recoveryAttempter] ;
if (!recoveryAttempter) {
NSURL* recoveryAttempterUrl = [[self userInfo] objectForKey:SSYRecoveryAttempterUrlErrorKey] ;
if (recoveryAttempterUrl) {
// recoveryOption NSAlertAlternateReturn is assumed to mean "Cancel".
if (recoveryOption == NSAlertAlternateReturn) {
return nil ;
}
recoveryAttempter = [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:recoveryAttempterUrl
display:NO
error:error_p] ;
}
else if (!recoveryAttempter) {
if ([[[self userInfo] objectForKey:SSYRecoveryAttempterIsAppDelegateErrorKey] boolValue]) {
recoveryAttempter = [NSApp delegate] ;
}
}
}
return recoveryAttempter ;
}
@end