forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSError+LowLevel.m
98 lines (87 loc) · 3.69 KB
/
NSError+LowLevel.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
#import "NSError+LowLevel.h"
__attribute__((visibility("default"))) NSString* const SSYAppleScriptErrorDomain = @"SSYAppleScriptErrorDomain" ;
@implementation NSError (LowLevel)
+ (NSError*)errorWithMacErrorCode:(OSStatus)code {
NSString* domain = @"NSError_LowLevel_ErrorDomain" ;
NSString* descString = nil ;
if ((code > 0) && (code < 15)) {
domain = @"CFStreamErrorDomain" ;
NSInteger intCode = (NSInteger)code ;
descString = [NSString stringWithFormat:
@"CFStreamError code %ld",
(long)intCode] ;
}
else if (code < kPOSIXErrorBase) {
domain = NSOSStatusErrorDomain ;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
const char* cString = GetMacOSStatusCommentString(code) ;
#else
const char* cString = NULL ;
#endif
if (cString) {
descString = [NSString stringWithUTF8String:cString] ;
}
else {
descString = [NSString stringWithFormat:
@"OSStatus error code %ld. See MacErrors.h",
(long)code] ;
}
}
else if (code <= kPOSIXErrorEOPNOTSUPP) {
domain = NSPOSIXErrorDomain ;
code -= kPOSIXErrorBase ;
descString = [NSString stringWithUTF8String:strerror(code)] ;
}
// I couldn't find any info on NSMachErrorDomain values
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
descString, NSLocalizedDescriptionKey, // may be nil
nil] ;
NSError* error = [NSError errorWithDomain:domain
code:code
userInfo:userInfo] ;
return error ;
}
+ (NSError*)errorWithPosixErrorCode:(OSStatus)code {
return [self errorWithMacErrorCode:(code+kPOSIXErrorBase)] ;
}
+ (NSError*)errorWithHTTPStatusCode:(NSInteger)code
prettyFunction:(const char*)prettyFunction {
NSString* methodName = [NSString stringWithCString:prettyFunction
encoding:NSASCIIStringEncoding] ;
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSHTTPURLResponse localizedStringForStatusCode:code], NSLocalizedDescriptionKey,
methodName, @"Method Name",
nil] ;
NSError* error = [NSError errorWithDomain:@"HttpStatusErrorDomain"
code:code
userInfo:userInfo] ;
return error ;
}
+ (NSError*)errorWithAppleScriptErrorDictionary:(NSDictionary*)dic {
if (!dic) {
return nil ;
}
NSInteger code = [[dic objectForKey:NSAppleScriptErrorNumber] integerValue] ;
NSString* localizedDescription = [dic objectForKey:NSAppleScriptErrorBriefMessage] ;
// The following is probably not very good, but the best I can think of
NSString* localizedFailureReason = [dic objectForKey:NSAppleScriptErrorMessage] ;
NSString* appName = [dic objectForKey:NSAppleScriptErrorAppName] ;
if (!appName) {
appName = @"Sorry, was not specified" ;
}
if (!localizedDescription) {
localizedDescription = @"Unknown error" ;
}
NSString* explanation = @"This error was translated from error info of an attempted AppleScript execution. Its code = the NSAppleScriptErrorNumber." ;
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
appName, @"From application", // won't be nil
explanation, @"ReadMe", // won't be nil
localizedDescription, NSLocalizedDescriptionKey, // won't be nil
[[NSProcessInfo processInfo] processName], @"Process receiving error", // will be, for example, "BookMacster" or "Sheep-Sys-Worker"
localizedFailureReason, NSLocalizedFailureReasonErrorKey, // may be nil
nil] ;
return [NSError errorWithDomain:SSYAppleScriptErrorDomain // Seems like Apple should provide a constant for this
code:code
userInfo:userInfo] ;
}
@end