forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSError+InfoAccess.m
executable file
·293 lines (245 loc) · 7.75 KB
/
NSError+InfoAccess.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
#import "NSError+InfoAccess.h"
#import "NSDictionary+SimpleMutations.h"
#import "SSY_ARC_OR_NO_ARC.h"
NSString* const SSYMethodNameErrorKey = @"Method Name" ;
NSString* const SSYLocalizedTitleErrorKey = @"Localized Title" ;
NSString* const SSYUnderlyingExceptionErrorKey = @"Underlying Exception" ;
NSString* const SSYTimestampErrorKey = @"Timestamp" ;
NSString* const SSYHttpStatusCodeErrorKey = @"HTTP Status Code" ;
@implementation NSError (InfoAccess)
- (NSString*)uniqueKeyForBaseKey:(NSString*)baseKey
sequenceNumber:(NSInteger)sequenceNumber {
NSString* uniqueKey ;
if (sequenceNumber == 0) {
uniqueKey = baseKey ;
}
else {
uniqueKey = [NSString stringWithFormat:
@"%@-%02ld",
[baseKey description],
(long)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*)errorByAppendingText:(NSString*)moreText
toValueForKey:(NSString*)key {
NSError* answer ;
if (moreText) {
NSString* text = [[self userInfo] objectForKey:key] ;
if (text) {
text = [NSString stringWithFormat:
@"%@ %@",
text,
moreText] ;
}
else {
text = moreText ;
}
answer = [self errorByOverwritingUserInfoObject:text
forKey:key] ;
}
else {
answer = self ;
}
return answer ;
}
- (NSError*)errorByAppendingLocalizedDescription:(NSString*)moreText {
return [self errorByAppendingText:moreText
toValueForKey:NSLocalizedDescriptionKey] ;
}
- (NSError*)errorByAppendingLocalizedFailureReason:(NSString*)moreText {
return [self errorByAppendingText:moreText
toValueForKey:NSLocalizedFailureReasonErrorKey] ;
}
- (NSError*)errorByAppendingLocalizedRecoverySuggestion:(NSString*)moreText {
return [self errorByAppendingText:moreText
toValueForKey:NSLocalizedRecoverySuggestionErrorKey] ;
}
- (NSError*)errorByAddingPrettyFunction:(const char*)prettyFunction {
NSError* error = nil ;
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*)errorByAddingLocalizedRecoveryOptions:(NSArray*)recoveryOptions {
return [self errorByAddingUserInfoObject:recoveryOptions
forKey:NSLocalizedRecoveryOptionsErrorKey] ;
}
- (NSError*)errorByAddingHelpAnchor:(NSString*)helpAnchor {
return [self errorByAddingUserInfoObject:helpAnchor
forKey:NSHelpAnchorErrorKey] ;
}
- (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] ;
#if NO_ARC
[exceptionInfo release] ;
#endif
return [self errorByAddingUserInfoObject:info
forKey:SSYUnderlyingExceptionErrorKey] ;
}
- (NSError*)errorByAddingLocalizedTitle:(NSString*)title {
return [self errorByAddingUserInfoObject:title
forKey:SSYLocalizedTitleErrorKey] ;
}
- (NSString*)localizedTitle {
NSString* title = [[self userInfo] objectForKey:SSYLocalizedTitleErrorKey] ;
return title ;
}
- (NSError*)errorByRemovingRecoveryAttempter {
NSMutableDictionary* userInfo = [[self userInfo] mutableCopy] ;
[userInfo removeObjectForKey:NSRecoveryAttempterErrorKey] ;
NSError* error = [NSError errorWithDomain:[self domain]
code:[self code]
userInfo:userInfo] ;
#if NO_ARC
[userInfo release] ;
#endif
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] ;
}
@end