Skip to content

Commit d92a31d

Browse files
author
Leo Vandriel
committed
add underlying error reason code
1 parent f6b9ff9 commit d92a31d

File tree

7 files changed

+42
-26
lines changed

7 files changed

+42
-26
lines changed

CHANGLOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Change Log
33

44
### master (unreleased)
55

6+
* Add underlying error reason code
7+
68
### 0.6.1 (2015-01-14)
79

810
* Add SSL handshake error codes

Classes/NWPushFeedback.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ - (BOOL)readTokenData:(NSData **)token date:(NSDate **)date error:(NSError *__au
7373
return read;
7474
}
7575
if (length != data.length) {
76-
return [NWErrorUtil noWithErrorCode:kNWErrorFeedbackLength error:error];
76+
return [NWErrorUtil noWithErrorCode:kNWErrorFeedbackLength reason:length error:error];
7777
}
7878
uint32_t time = 0;
7979
[data getBytes:&time range:NSMakeRange(0, 4)];
@@ -82,7 +82,7 @@ - (BOOL)readTokenData:(NSData **)token date:(NSDate **)date error:(NSError *__au
8282
[data getBytes:&l range:NSMakeRange(4, 2)];
8383
NSUInteger tokenLength = htons(l);
8484
if (tokenLength != NWTokenMaxSize) {
85-
return [NWErrorUtil noWithErrorCode:kNWErrorFeedbackTokenLength error:error];
85+
return [NWErrorUtil noWithErrorCode:kNWErrorFeedbackTokenLength reason:tokenLength error:error];
8686
}
8787
*token = [data subdataWithRange:NSMakeRange(6, length - 6)];
8888
return YES;

Classes/NWPusher.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ - (BOOL)pushNotification:(NWNotification *)notification type:(NWNotificationType
8282
return written;
8383
}
8484
if (length != data.length) {
85-
return [NWErrorUtil noWithErrorCode:kNWErrorPushWriteFail error:error];
85+
return [NWErrorUtil noWithErrorCode:kNWErrorPushWriteFail reason:length error:error];
8686
}
8787
return YES;
8888
}
@@ -101,7 +101,7 @@ - (BOOL)readFailedIdentifier:(NSUInteger *)identifier apnError:(NSError *__autor
101101
uint8_t command = 0;
102102
[data getBytes:&command range:NSMakeRange(0, 1)];
103103
if (command != 8) {
104-
return [NWErrorUtil noWithErrorCode:kNWErrorPushResponseCommand error:error];
104+
return [NWErrorUtil noWithErrorCode:kNWErrorPushResponseCommand reason:command error:error];
105105
}
106106
uint8_t status = 0;
107107
[data getBytes:&status range:NSMakeRange(1, 1)];
@@ -118,7 +118,7 @@ - (BOOL)readFailedIdentifier:(NSUInteger *)identifier apnError:(NSError *__autor
118118
case 7: [NWErrorUtil noWithErrorCode:kNWErrorAPNInvalidPayloadSize error:apnError]; break;
119119
case 8: [NWErrorUtil noWithErrorCode:kNWErrorAPNInvalidTokenContent error:apnError]; break;
120120
case 10: [NWErrorUtil noWithErrorCode:kNWErrorAPNShutdown error:apnError]; break;
121-
default: [NWErrorUtil noWithErrorCode:kNWErrorAPNUnknownErrorCode error:apnError]; break;
121+
default: [NWErrorUtil noWithErrorCode:kNWErrorAPNUnknownErrorCode reason:status error:apnError]; break;
122122
}
123123
return YES;
124124
}

Classes/NWSSLConnection.m

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ - (BOOL)connectSocketWithError:(NSError *__autoreleasing *)error
6868
{
6969
int sock = socket(AF_INET, SOCK_STREAM, 0);
7070
if (sock < 0) {
71-
return [NWErrorUtil noWithErrorCode:kNWErrorSocketCreate error:error];
71+
return [NWErrorUtil noWithErrorCode:kNWErrorSocketCreate reason:sock error:error];
7272
}
7373
struct sockaddr_in addr;
7474
memset(&addr, 0, sizeof(struct sockaddr_in));
@@ -83,15 +83,15 @@ - (BOOL)connectSocketWithError:(NSError *__autoreleasing *)error
8383
addr.sin_family = AF_INET;
8484
int conn = connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
8585
if (conn < 0) {
86-
return [NWErrorUtil noWithErrorCode:kNWErrorSocketConnect error:error];
86+
return [NWErrorUtil noWithErrorCode:kNWErrorSocketConnect reason:conn error:error];
8787
}
8888
int cntl = fcntl(sock, F_SETFL, O_NONBLOCK);
8989
if (cntl < 0) {
90-
return [NWErrorUtil noWithErrorCode:kNWErrorSocketFileControl error:error];
90+
return [NWErrorUtil noWithErrorCode:kNWErrorSocketFileControl reason:cntl error:error];
9191
}
9292
int set = 1, sopt = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
9393
if (sopt < 0) {
94-
return [NWErrorUtil noWithErrorCode:kNWErrorSocketOptions error:error];
94+
return [NWErrorUtil noWithErrorCode:kNWErrorSocketOptions reason:sopt error:error];
9595
}
9696
_socket = sock;
9797
return YES;
@@ -105,19 +105,19 @@ - (BOOL)connectSSLWithError:(NSError *__autoreleasing *)error
105105
}
106106
OSStatus setio = SSLSetIOFuncs(context, NWSSLRead, NWSSLWrite);
107107
if (setio != errSecSuccess) {
108-
return [NWErrorUtil noWithErrorCode:kNWErrorSSLIOFuncs error:error];
108+
return [NWErrorUtil noWithErrorCode:kNWErrorSSLIOFuncs reason:setio error:error];
109109
}
110110
OSStatus setconn = SSLSetConnection(context, (SSLConnectionRef)(NSInteger)_socket);
111111
if (setconn != errSecSuccess) {
112-
return [NWErrorUtil noWithErrorCode:kNWErrorSSLConnection error:error];
112+
return [NWErrorUtil noWithErrorCode:kNWErrorSSLConnection reason:setconn error:error];
113113
}
114114
OSStatus setpeer = SSLSetPeerDomainName(context, _host.UTF8String, strlen(_host.UTF8String));
115115
if (setpeer != errSecSuccess) {
116-
return [NWErrorUtil noWithErrorCode:kNWErrorSSLPeerDomainName error:error];
116+
return [NWErrorUtil noWithErrorCode:kNWErrorSSLPeerDomainName reason:setpeer error:error];
117117
}
118118
OSStatus setcert = SSLSetCertificate(context, (__bridge CFArrayRef)@[_identity]);
119119
if (setcert != errSecSuccess) {
120-
return [NWErrorUtil noWithErrorCode:kNWErrorSSLCertificate error:error];
120+
return [NWErrorUtil noWithErrorCode:kNWErrorSSLCertificate reason:setcert error:error];
121121
}
122122
_context = context;
123123
return YES;
@@ -141,7 +141,7 @@ - (BOOL)handshakeSSLWithError:(NSError *__autoreleasing *)error
141141
case errSSLClientCertRequested: return [NWErrorUtil noWithErrorCode:kNWErrorSSLHandshakeClientCertRequested error:error];
142142
case errSSLServerAuthCompleted: return [NWErrorUtil noWithErrorCode:kNWErrorSSLHandshakeServerAuthCompleted error:error];
143143
}
144-
return [NWErrorUtil noWithErrorCode:kNWErrorSSLHandshakeFail error:error];
144+
return [NWErrorUtil noWithErrorCode:kNWErrorSSLHandshakeFail reason:status error:error];
145145
}
146146

147147
- (void)disconnect
@@ -166,7 +166,7 @@ - (BOOL)read:(NSMutableData *)data length:(NSUInteger *)length error:(NSError *_
166166
case errSSLClosedAbort: return [NWErrorUtil noWithErrorCode:kNWErrorReadClosedAbort error:error];
167167
case errSSLClosedGraceful: return [NWErrorUtil noWithErrorCode:kNWErrorReadClosedGraceful error:error];
168168
}
169-
return [NWErrorUtil noWithErrorCode:kNWErrorReadFail error:error];
169+
return [NWErrorUtil noWithErrorCode:kNWErrorReadFail reason:status error:error];
170170
}
171171

172172
- (BOOL)write:(NSData *)data length:(NSUInteger *)length error:(NSError *__autoreleasing *)error
@@ -182,7 +182,7 @@ - (BOOL)write:(NSData *)data length:(NSUInteger *)length error:(NSError *__autor
182182
case errSSLClosedAbort: return [NWErrorUtil noWithErrorCode:kNWErrorWriteClosedAbort error:error];
183183
case errSSLClosedGraceful: return [NWErrorUtil noWithErrorCode:kNWErrorWriteClosedGraceful error:error];
184184
}
185-
return [NWErrorUtil noWithErrorCode:kNWErrorWriteFail error:error];
185+
return [NWErrorUtil noWithErrorCode:kNWErrorWriteFail reason:status error:error];
186186
}
187187

188188
@end

Classes/NWSecTools.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ + (NWIdentityRef)identityWithPKCS12Data:(NSData *)pkcs12 password:(NSString *)pa
3838
return [NWErrorUtil nilWithErrorCode:kNWErrorPKCS12NoItems error:error];
3939
}
4040
if (identities.count > 1) {
41-
return [NWErrorUtil nilWithErrorCode:kNWErrorPKCS12MultipleItems error:error];
41+
return [NWErrorUtil nilWithErrorCode:kNWErrorPKCS12MultipleItems reason:identities.count error:error];
4242
}
4343
return identities.lastObject;
4444
}
@@ -208,7 +208,7 @@ + (NWCertificateRef)certificateWithIdentity:(NWIdentityRef)identity error:(NSErr
208208
OSStatus status = identity ? SecIdentityCopyCertificate((__bridge SecIdentityRef)identity, &cert) : errSecParam;
209209
NWCertificateRef certificate = CFBridgingRelease(cert);
210210
if (status != errSecSuccess || !cert) {
211-
return [NWErrorUtil nilWithErrorCode:kNWErrorIdentityCopyCertificate error:error];
211+
return [NWErrorUtil nilWithErrorCode:kNWErrorIdentityCopyCertificate reason:status error:error];
212212
}
213213
return certificate;
214214
}
@@ -219,7 +219,7 @@ + (NWKeyRef)keyWithIdentity:(NWIdentityRef)identity error:(NSError *__autoreleas
219219
OSStatus status = identity ? SecIdentityCopyPrivateKey((__bridge SecIdentityRef)identity, &k) : errSecParam;
220220
NWKeyRef key = CFBridgingRelease(k);
221221
if (status != errSecSuccess || !k) {
222-
return [NWErrorUtil nilWithErrorCode:kNWErrorIdentityCopyPrivateKey error:error];
222+
return [NWErrorUtil nilWithErrorCode:kNWErrorIdentityCopyPrivateKey reason:status error:error];
223223
}
224224
return key;
225225
}
@@ -238,7 +238,7 @@ + (NSArray *)allIdentitiesWithPKCS12Data:(NSData *)data password:(NSString *)pas
238238
case errSecPkcs12VerifyFailure: return [NWErrorUtil nilWithErrorCode:kNWErrorPKCS12Password error:error];
239239
#endif
240240
}
241-
return [NWErrorUtil nilWithErrorCode:kNWErrorPKCS12Import error:error];
241+
return [NWErrorUtil nilWithErrorCode:kNWErrorPKCS12Import reason:status error:error];
242242
}
243243
return dicts;
244244
}
@@ -251,7 +251,7 @@ + (NSArray *)allKeychainCertificatesWithError:(NSError *__autoreleasing *)error
251251
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)options, (CFTypeRef *)&certs);
252252
NSArray *certificates = CFBridgingRelease(certs);
253253
if (status != errSecSuccess || !certs) {
254-
return [NWErrorUtil nilWithErrorCode:kNWErrorKeychainCopyMatching error:error];
254+
return [NWErrorUtil nilWithErrorCode:kNWErrorKeychainCopyMatching reason:status error:error];
255255
}
256256
return certificates;
257257
}
@@ -266,7 +266,7 @@ + (NWIdentityRef)keychainIdentityWithCertificate:(NWCertificateRef)certificate e
266266
switch (status) {
267267
case errSecItemNotFound: return [NWErrorUtil nilWithErrorCode:kNWErrorKeychainItemNotFound error:error];
268268
}
269-
return [NWErrorUtil nilWithErrorCode:kNWErrorKeychainCreateIdentity error:error];
269+
return [NWErrorUtil nilWithErrorCode:kNWErrorKeychainCreateIdentity reason:status error:error];
270270
}
271271
return identity;
272272
}

Classes/NWType.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ typedef NS_ENUM(NSInteger, NWError) {
164164

165165
/** Assigns the error with provided code and associated description, for returning `NO`. */
166166
+ (BOOL)noWithErrorCode:(NWError)code error:(NSError **)error;
167+
+ (BOOL)noWithErrorCode:(NWError)code reason:(NSInteger)reason error:(NSError **)error;
167168

168169
/** Assigns the error with provided code and associated description, for returning `nil`. */
169170
+ (id)nilWithErrorCode:(NWError)code error:(NSError **)error;
171+
+ (id)nilWithErrorCode:(NWError)code reason:(NSInteger)reason error:(NSError **)error;
170172

171173
@end

Classes/NWType.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,35 @@ + (NSString *)stringWithCode:(NWError)code
8585

8686
#pragma mark - Helpers
8787

88-
+ (NSError *)errorWithErrorCode:(NWError)code
88+
+ (NSError *)errorWithErrorCode:(NWError)code reason:(NSInteger)reason
8989
{
90-
NSDictionary *info = @{ NSLocalizedDescriptionKey: [self stringWithCode:code] };
90+
NSString *description = [self stringWithCode:code];
91+
if (reason) description = [NSString stringWithFormat:@"%@ (%li)", description, reason];
92+
NSDictionary *info = @{ NSLocalizedDescriptionKey:description };
9193
return [NSError errorWithDomain:@"NWPusherErrorDomain" code:code userInfo:info];
9294
}
9395

9496
+ (BOOL)noWithErrorCode:(NWError)code error:(NSError *__autoreleasing *)error
97+
{
98+
return [self noWithErrorCode:code reason:0 error:error];
99+
}
100+
101+
+ (BOOL)noWithErrorCode:(NWError)code reason:(NSInteger)reason error:(NSError *__autoreleasing *)error
95102
{
96103
NSAssert(code != kNWErrorNone, @"code != kNWErrorNone");
97-
if (error) *error = [self errorWithErrorCode:code];
104+
if (error) *error = [self errorWithErrorCode:code reason:reason];
98105
return NO;
99106
}
100107

101108
+ (id)nilWithErrorCode:(NWError)code error:(NSError *__autoreleasing *)error
109+
{
110+
return [self nilWithErrorCode:code reason:0 error:error];
111+
}
112+
113+
+ (id)nilWithErrorCode:(NWError)code reason:(NSInteger)reason error:(NSError *__autoreleasing *)error
102114
{
103115
NSAssert(code != kNWErrorNone, @"code != kNWErrorNone");
104-
if (error) *error = [self errorWithErrorCode:code];
116+
if (error) *error = [self errorWithErrorCode:code reason:reason];
105117
return nil;
106118
}
107119

0 commit comments

Comments
 (0)