Skip to content

Commit

Permalink
Add a description to errors, even if they have an underlyingError wit…
Browse files Browse the repository at this point in the history
…h a description
  • Loading branch information
WilliamDenniss committed Jun 29, 2018
1 parent a79e572 commit b9d3ce7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
69 changes: 52 additions & 17 deletions Source/OIDAuthorizationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ - (void)presentAuthorizationWithExternalUserAgent:(id<OIDExternalUserAgent>)exte

- (void)cancel {
[_externalUserAgent dismissExternalUserAgentAnimated:YES completion:^{
NSError *error = [OIDErrorUtilities
errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
underlyingError:nil
description:nil];
NSError *error = [OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
underlyingError:nil
description:@"Authorization flow was cancelled."];
[self didFinishWithResponse:nil error:error];
}];
}
Expand Down Expand Up @@ -209,9 +208,13 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// If we got any sort of error, just report it.
if (error || !data) {
NSString *errorDescription =
[NSString stringWithFormat:@"Connection error fetching discovery document '%@': %@.",
discoveryURL,
error.localizedDescription];
error = [OIDErrorUtilities errorWithCode:OIDErrorCodeNetworkError
underlyingError:error
description:error.localizedDescription];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
Expand All @@ -225,9 +228,14 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL
if (urlResponse.statusCode != 200) {
NSError *URLResponseError = [OIDErrorUtilities HTTPErrorWithHTTPResponse:urlResponse
data:data];
NSString *errorDescription =
[NSString stringWithFormat:@"Non-200 HTTP response (%d) fetching discovery document "
"'%@'.",
(int)urlResponse.statusCode,
discoveryURL];
error = [OIDErrorUtilities errorWithCode:OIDErrorCodeNetworkError
underlyingError:URLResponseError
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
Expand All @@ -238,9 +246,13 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL
OIDServiceDiscovery *discovery =
[[OIDServiceDiscovery alloc] initWithJSONData:data error:&error];
if (error || !discovery) {
NSString *errorDescription =
[NSString stringWithFormat:@"JSON error parsing document at '%@': %@",
discoveryURL,
error.localizedDescription];
error = [OIDErrorUtilities errorWithCode:OIDErrorCodeNetworkError
underlyingError:error
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
Expand Down Expand Up @@ -294,10 +306,14 @@ + (void)performTokenRequest:(OIDTokenRequest *)request
NSError *_Nullable error) {
if (error) {
// A network error or server error occurred.
NSString *errorDescription =
[NSString stringWithFormat:@"Connection error making token request to '%@': %@.",
URLRequest.URL,
error.localizedDescription];
NSError *returnedError =
[OIDErrorUtilities errorWithCode:OIDErrorCodeNetworkError
underlyingError:error
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, returnedError);
});
Expand Down Expand Up @@ -331,11 +347,15 @@ + (void)performTokenRequest:(OIDTokenRequest *)request
}
}

// not an OAuth error, just a generic server error
// Status code indicates this is an error, but not an RFC6749 Section 5.2 error.
NSString *errorDescription =
[NSString stringWithFormat:@"Non-200 HTTP response (%d) making token request to '%@'.",
(int)statusCode,
URLRequest.URL];
NSError *returnedError =
[OIDErrorUtilities errorWithCode:OIDErrorCodeServerError
underlyingError:serverError
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, returnedError);
});
Expand All @@ -347,10 +367,13 @@ + (void)performTokenRequest:(OIDTokenRequest *)request
[NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonDeserializationError];
if (jsonDeserializationError) {
// A problem occurred deserializing the response/JSON.
NSString *errorDescription =
[NSString stringWithFormat:@"JSON error parsing token response: %@",
jsonDeserializationError.localizedDescription];
NSError *returnedError =
[OIDErrorUtilities errorWithCode:OIDErrorCodeJSONDeserializationError
underlyingError:jsonDeserializationError
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, returnedError);
});
Expand All @@ -364,7 +387,7 @@ + (void)performTokenRequest:(OIDTokenRequest *)request
NSError *returnedError =
[OIDErrorUtilities errorWithCode:OIDErrorCodeTokenResponseConstructionError
underlyingError:jsonDeserializationError
description:nil];
description:@"Token response invalid."];
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, returnedError);
});
Expand Down Expand Up @@ -520,9 +543,13 @@ + (void)performRegistrationRequest:(OIDRegistrationRequest *)request
NSError *_Nullable error) {
if (error) {
// A network error or server error occurred.
NSString *errorDescription =
[NSString stringWithFormat:@"Connection error making registration request to '%@': %@.",
URLRequest.URL,
error.localizedDescription];
NSError *returnedError = [OIDErrorUtilities errorWithCode:OIDErrorCodeNetworkError
underlyingError:error
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, returnedError);
});
Expand Down Expand Up @@ -558,9 +585,14 @@ + (void)performRegistrationRequest:(OIDRegistrationRequest *)request
}

// not an OAuth error, just a generic server error
NSString *errorDescription =
[NSString stringWithFormat:@"Non-200/201 HTTP response (%d) making registration request "
"to '%@'.",
(int)HTTPURLResponse.statusCode,
URLRequest.URL];
NSError *returnedError = [OIDErrorUtilities errorWithCode:OIDErrorCodeServerError
underlyingError:serverError
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, returnedError);
});
Expand All @@ -572,9 +604,12 @@ + (void)performRegistrationRequest:(OIDRegistrationRequest *)request
[NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonDeserializationError];
if (jsonDeserializationError) {
// A problem occurred deserializing the response/JSON.
NSString *errorDescription =
[NSString stringWithFormat:@"JSON error parsing registration response: %@",
jsonDeserializationError.localizedDescription];
NSError *returnedError = [OIDErrorUtilities errorWithCode:OIDErrorCodeJSONDeserializationError
underlyingError:jsonDeserializationError
description:nil];
description:errorDescription];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, returnedError);
});
Expand All @@ -588,8 +623,8 @@ + (void)performRegistrationRequest:(OIDRegistrationRequest *)request
// A problem occurred constructing the registration response from the JSON.
NSError *returnedError =
[OIDErrorUtilities errorWithCode:OIDErrorCodeRegistrationResponseConstructionError
underlyingError:jsonDeserializationError
description:nil];
underlyingError:nil
description:@"Registration response invalid."];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, returnedError);
});
Expand Down
2 changes: 1 addition & 1 deletion Source/OIDServiceDiscovery.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ - (nullable instancetype)initWithJSONData:(NSData *)serviceDiscoveryJSONData
if (!json || jsonError) {
*error = [OIDErrorUtilities errorWithCode:OIDErrorCodeJSONDeserializationError
underlyingError:jsonError
description:nil];
description:jsonError.localizedDescription];
return nil;
}
return [self initWithDictionary:json error:error];
Expand Down
4 changes: 2 additions & 2 deletions Source/iOS/OIDExternalUserAgentIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ - (BOOL)presentExternalUserAgentRequest:(id<OIDExternalUserAgentRequest>)request
NSError *safariError =
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
underlyingError:error
description:nil];
description:@"User cancelled."];
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
}
}];
Expand Down Expand Up @@ -185,7 +185,7 @@ - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller NS_AV
[self cleanUp];
NSError *error = [OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
underlyingError:nil
description:nil];
description:@"No external user agent flow in progress."];
[session failExternalUserAgentFlowWithError:error];
}

Expand Down

0 comments on commit b9d3ce7

Please sign in to comment.