2 // NSError+KCCreationHelpers.m
7 #import <Foundation/Foundation.h>
9 #import <NSError+KCCreationHelpers.h>
11 static NSString* coreCryptoDomain = @"kSecCoreCryptoDomain";
12 static NSString* srpDomain = @"com.apple.security.srp";
14 static NSDictionary* UserInfoFromVA(NSString*description, va_list va)
15 NS_FORMAT_FUNCTION(1, 0);
17 static NSDictionary* UserInfoFromVA(NSString*description, va_list va) {
18 return @{NSLocalizedDescriptionKey:[[NSString alloc] initWithFormat:description
22 // We should get this from SecCFError.h and Security.framework..
23 bool CoreCryptoError(int cc_result, NSError** error, NSString* description, ...)
25 bool failed = cc_result != 0;
27 if (failed && error && !*error) {
29 va_start(va, description);
30 *error = [NSError errorWithCoreCryptoStatus:cc_result
31 description:description
39 bool OSStatusError(OSStatus status, NSError** error, NSString*description, ...) {
40 bool failed = status != 0;
42 if (failed && error && !*error) {
44 va_start(va, description);
45 *error = [NSError errorWithOSStatus:status
46 description:description
54 bool RequirementError(bool requirement, NSError** error, NSString*description, ...) {
55 bool failed = !requirement;
57 if (failed && error && !*error) {
59 va_start(va, description);
60 *error = [NSError errorWithOSStatus:-50
61 description:description
70 @implementation NSError(KCCreationHelpers)
72 + (instancetype) errorWithOSStatus:(OSStatus) status
73 userInfo:(NSDictionary *)dict {
74 return [[NSError alloc] initWithOSStatus:status userInfo:dict];
77 - (instancetype) initWithOSStatus:(OSStatus) status
78 userInfo:(NSDictionary *)dict {
79 return [self initWithDomain:NSOSStatusErrorDomain code:status userInfo:dict];
82 + (instancetype) errorWithOSStatus:(OSStatus) status
83 description:(NSString*)description
85 return [[NSError alloc] initWithOSStatus:status description:description args:va];
88 - (instancetype) initWithOSStatus:(OSStatus) status
89 description:(NSString*)description
91 return [self initWithOSStatus:status
92 userInfo:UserInfoFromVA(description, va)];
95 + (instancetype) errorWithCoreCryptoStatus:(int) status
96 userInfo:(NSDictionary *)dict {
97 return [[NSError alloc] initWithCoreCryptoStatus:status userInfo:dict];
101 - (instancetype) initWithCoreCryptoStatus:(int) status
102 userInfo:(NSDictionary *)dict {
103 return [self initWithDomain:coreCryptoDomain code:status userInfo:dict];
106 + (instancetype) errorWithCoreCryptoStatus:(int) status
107 description:(NSString*)description
109 return [[NSError alloc] initWithCoreCryptoStatus:status description:description args:va];
112 - (instancetype) initWithCoreCryptoStatus:(int) status
113 description:(NSString*)description
115 return [self initWithCoreCryptoStatus:status
116 userInfo:UserInfoFromVA(description, va)];