Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include request/response details in mapped error, when possible #1803

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

sadlerjw
Copy link
Contributor

Summary

This allows RestKit consumers to perform an error mapping on the response, but still inspect the HTTP status code of the response, or any other properties of the request and response objects.

Details

When a non-200-series HTTP status code is received and there is no object mapping set up to handle it, an NSError is generated that includes the response string, the request's URL, and the request and response objects themselves. This allows consumers of RestKit to inspect these objects (for instance, to find out the
status code) in order to determine the correct course of action. (See RKHTTPRequestOperation's error method)

When an object mapping does exist for non 200-series status codes, the NSError object does not include this information - it only includes the mapped error(s) and a localized description. This leaves RestKit consumers no idea of what the HTTP status code was, which is usually important information when determining how to react to an error.

This commit adds URL, request, and response to the NSError's userInfo, but does not add responseString, because the same information is conveyed by the mapped error object(s) (which are stored in userInfo[RKObjectMapperErrorObjectsKey].

We maintain backwards compatibility by implementing this functionality in a new function, which is invoked using nil for request and response when the old function is called. (There are no internal calls to the old function, but it is listed in a .h file so some RestKit consumers may be calling it.)

@segiddins
Copy link
Member

@sadlerjw why not just use NSUndelyingErrorKey?

@sadlerjw
Copy link
Contributor Author

@segiddins I don't think there is an underlying error in this case. Happy to be proven wrong though :)
(The NSError that is generated if there is no error mapping is not generated at all if there is an error mapping, so we can't use it as the underlying error.)

@@ -263,3 +263,4 @@
@see `RKErrorMessage`
*/
NSError *RKErrorFromMappingResult(RKMappingResult *mappingResult);
NSError *RKErrorFromMappingResultAndRequestResponse(RKMappingResult *mappingResult, NSURLRequest *request, NSHTTPURLResponse *response);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this a static method internal to the .m

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue that this new function should be called generally instead of RKErrorFromMappingResult. In fact, I suggest that we deprecate RKErrorFromMappingResult and keep the new function listed here as a public function.

Alternatively, if you want to discourage people from calling these functions, we could make the new one private, as you suggest, and deprecate the old function.

If you are opposed to these suggestions, I'll go ahead and implement your original idea - just wanted to suggest a possible alternative.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could make the new one private, as you suggest, and deprecate the old function

👍

@segiddins
Copy link
Member

Rebase this onto development and I'll merge it

When a non-200-series HTTP status code is received and there is no object
mapping set up to handle it, an NSError is generated that includes the response
string, the request's URL, and the request and response objects themselves. This
allows consumers of RestKit to inspect these objects (for instance, to find out the
status code) in order to determine the correct course of action.
(See RKHTTPRequestOperation's `error` method)

When an object mapping does exist for non 200-series status codes, the mapped
error does not include this information - it only includes the mapped error(s) and
a localized description. This leaves RestKit consumers no idea of what the HTTP
status code was, which is usually important information when determining how to
react to an error.

This commit adds URL, request, and response to the NSError object, but does not
add responseString, because the same information is conveyed by the mapped
error object(s).

We maintain backwards compatibility by using `nil` for request and response when
using the old function.
@sadlerjw
Copy link
Contributor Author

Updated

@segiddins
Copy link
Member

Just realized this never got test coverage :/ If you add that, I can merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants