forked from yannisroy/MYUtilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMYErrorUtils.m
260 lines (223 loc) · 8.91 KB
/
MYErrorUtils.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
//
// MYErrorUtils.m
// MYUtilities
//
// Created by Jens Alfke on 2/25/09.
// Copyright 2009 Jens Alfke. All rights reserved.
//
#import "MYErrorUtils.h"
#import "Test.h"
#import "CollectionUtils.h"
#import <Foundation/Foundation.h>
#if MYERRORUTILS_USE_SECURITY_API
#import <Security/SecBase.h>
#endif
NSString* const MYErrorDomain = @"MYErrorDomain";
static NSError *MYMakeErrorV( int errorCode, NSString *domain, NSString *message, va_list args )
{
message = [[NSString alloc] initWithFormat: message arguments: args];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
message, NSLocalizedDescriptionKey,
nil];
[message release];
return [NSError errorWithDomain: domain
code: errorCode
userInfo: userInfo];
}
NSError *MYError( int errorCode, NSString *domain, NSString *message, ... )
{
va_list args;
va_start(args,message);
NSError *error = MYMakeErrorV(errorCode,domain,message,args);
va_end(args);
return error;
}
BOOL MYReturnError( NSError **outError,
int errorCode, NSString *domain, NSString *messageFormat, ... )
{
if (errorCode) {
if (outError) {
va_list args;
va_start(args,messageFormat);
*outError = MYMakeErrorV(errorCode, domain, messageFormat, args);
va_end(args);
LogMY(@"MYReturnError: %@",*outError);
} else
LogMY(@"MYReturnError: %@/%i",domain,errorCode);
return NO;
} else
return YES;
}
BOOL MYMiscError( NSError **error, NSString *message, ... )
{
if (error) {
va_list args;
va_start(args,message);
*error = MYMakeErrorV(kMYErrorMisc,MYErrorDomain, message,args);
va_end(args);
}
return NO;
}
NSError *MYErrorFromErrno(void)
{
int err = errno;
if (!err)
return nil;
return MYError(err, NSPOSIXErrorDomain, @"%s", strerror(err));
}
static NSString* printableOSType( OSType t ) {
if (t < 0x20202020 || t > 0x7e7e7e7e)
return nil;
union {
OSType ostype;
unsigned char ch[4];
} buf;
buf.ostype = CFSwapInt32HostToBig(t);
for (int i=0; i<4; i++)
if (buf.ch[i] < 0x20 || buf.ch[i] > 0x7E)
return nil;
return [[[NSString alloc] initWithBytes: &buf.ch length: 4 encoding: NSMacOSRomanStringEncoding]
autorelease];
}
static NSString* printableErrorCode( NSInteger code ) {
if ((OSStatus)code < -99999)
return $sprintf(@"%u", (unsigned)code); // CSSM errors are huge unsigned values > 0x80000000
NSString *result = printableOSType((OSType)code);
if (result)
return result; // CoreAudio errors are OSTypes (4-char strings)
return $sprintf(@"%ld", (long)code); // Default: OSStatus and errno values are signed
}
static NSString* MYShortErrorDomainName( NSString *domain ) {
if ([domain hasPrefix: @"kCFErrorDomain"])
domain = [domain substringFromIndex: 14];
else {
if ([domain hasSuffix: @"ErrorDomain"])
domain = [domain substringToIndex: domain.length - 11];
if ([domain hasPrefix: @"NS"])
domain = [domain substringFromIndex: 2];
}
return domain;
}
NSString* MYErrorName( NSString *domain, NSInteger code ) {
if (code == 0)
return nil;
NSString *codeStr = printableErrorCode(code);
if (!domain)
return codeStr;
NSString *result = nil;
if ($equal(domain,NSPOSIXErrorDomain)) {
// Interpret POSIX errors via strerror
// (which unfortunately returns a description, not the name of the constant)
const char *name = strerror((int)code);
if (name) {
result = [NSString stringWithCString: name encoding: NSASCIIStringEncoding];
if ([result hasPrefix: @"Unknown error"])
result = nil;
}
}
#if !TARGET_OS_IPHONE || defined(__SEC_TYPES__)
else if ($equal(domain,NSOSStatusErrorDomain)) {
// If it's an OSStatus, check whether CarbonCore knows its name:
const char *name = NULL;
#if !TARGET_OS_IPHONE
#if (__MAC_OS_X_VERSION_MIN_REQUIRED < 1080)
name = GetMacOSStatusErrorString((int)code);
#endif
#endif
if (name && *name)
result = [NSString stringWithCString: name encoding: NSMacOSRomanStringEncoding];
else {
#if MYERRORUTILS_USE_SECURITY_API
result = (id) SecCopyErrorMessageString((OSStatus)code,NULL);
if (result) {
[NSMakeCollectable(result) autorelease];
if ([result hasPrefix: @"OSStatus "])
result = nil; // just a generic message
}
#endif
}
}
#endif
if (!result) {
// Look up errors in string files keyed by the domain name:
NSString *table = [@"MYError_" stringByAppendingString: domain];
result = [[NSBundle mainBundle] localizedStringForKey: codeStr value: @"?" table: table];
if ([result isEqualToString: @"?"])
result = nil;
}
codeStr = $sprintf(@"%@ %@", MYShortErrorDomainName(domain), codeStr);;
return result ? $sprintf(@"%@ (%@)", result, codeStr) : codeStr;
}
@implementation NSError (MYUtils)
- (NSError*) my_errorByPrependingMessage: (NSString*)message
{
if( message.length ) {
NSDictionary *oldUserInfo = self.userInfo;
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
if( oldUserInfo )
[userInfo addEntriesFromDictionary: oldUserInfo];
NSString *desc = [oldUserInfo objectForKey: NSLocalizedDescriptionKey];
if( desc )
message = $sprintf(@"%@: %@", message, desc);
[userInfo setObject: message forKey: NSLocalizedDescriptionKey];
return [NSError errorWithDomain: self.domain
code: self.code
userInfo: userInfo];
} else
return self;
}
- (NSString*) my_nameOfCode {
return MYErrorName(self.domain, self.code);
}
@end
TestCase(MYErrorUtils) {
CAssertEqual(printableOSType('abcd'), @"abcd");
CAssertEqual(printableOSType(' '), @" ");
CAssertEqual(printableOSType(0x7e7e7e7e), @"~~~~");
CAssertEqual(printableOSType(0x7e7F7e7e), nil);
CAssertEqual(printableOSType(0x7e0D7e7e), nil);
CAssertEqual(printableOSType(0), nil);
CAssertEqual(printableOSType((OSType)-123456), nil);
CAssertEqual(MYErrorName(nil,0), nil);
CAssertEqual(MYErrorName(nil,12345), @"12345");
CAssertEqual(MYErrorName(nil,1), @"1");
CAssertEqual(MYErrorName(nil,-1), @"-1");
CAssertEqual(MYErrorName(nil,12345), @"12345");
CAssertEqual(MYErrorName(nil,-12345), @"-12345");
CAssertEqual(MYErrorName(nil,2147549184u), @"2147549184"); // that's 0x80010000
CAssertEqual(MYErrorName(@"foobar",0), nil);
CAssertEqual(MYErrorName(@"foobar",'fmt?'), @"foobar fmt?");
CAssertEqual(MYErrorName(@"foobar",1), @"foobar 1");
CAssertEqual(MYErrorName(@"FoobarErrorDomain",-1), @"Foobar -1");
CAssertEqual(MYErrorName(@"NSFoobarErrorDomain",12345), @"Foobar 12345");
NSError *err;
err = [NSError errorWithDomain: NSPOSIXErrorDomain code: EPERM userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"Operation not permitted (POSIX 1)");
err = [NSError errorWithDomain: NSPOSIXErrorDomain code: 12345 userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"POSIX 12345");
#if !TARGET_OS_IPHONE
#if (__MAC_OS_X_VERSION_MIN_REQUIRED < 1080)
err = [NSError errorWithDomain: NSOSStatusErrorDomain code: paramErr userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"paramErr (OSStatus -50)");
err = [NSError errorWithDomain: NSOSStatusErrorDomain code: fnfErr userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"fnfErr (OSStatus -43)");
err = [NSError errorWithDomain: NSOSStatusErrorDomain code: -25291 userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"errKCNotAvailable / errSecNotAvailable (OSStatus -25291)");
#endif
#if MYERRORUTILS_USE_SECURITY_API
err = [NSError errorWithDomain: NSOSStatusErrorDomain code: -25260 userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"Passphrase is required for import/export. (OSStatus -25260)");
#endif
#endif
err = [NSError errorWithDomain: NSOSStatusErrorDomain code: 12345 userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"OSStatus 12345");
err = [NSError errorWithDomain: @"CSSMErrorDomain" code: 2147549184u userInfo: nil];
#if MYERRORUTILS_USE_SECURITY_API
CAssertEqual(err.my_nameOfCode, @"CSSM_CSSM_BASE_ERROR (CSSM 2147549184)");
// If that assertion fails, you probably need to add MYError_CSSMErrorDomain.strings to your target.
#else
CAssertEqual(err.my_nameOfCode, @"CSSM 2147549184");
#endif
err = [NSError errorWithDomain: (id)kCFErrorDomainCocoa code: 100 userInfo: nil];
CAssertEqual(err.my_nameOfCode, @"Cocoa 100");
}