Closed hamaluik closed 7 years ago
Another possible change is to just include the xhr as a field of the Error. For jQuery, you get the xhr when there is an error callback: http://api.jquery.com/jQuery.ajax/
But, to use what is available now, consider there a loop in the error handler that takes fields from the response and puts them into the error:
for (var key in response) error[key] = response[key]
where response is defined earlier by:
var response = (args.extract !== extract) ? args.extract(xhr, args) : args.deserialize(args.extract(xhr, args))
So, you could process the xhr result yourself in extract, and, if there is an error, indicate it some way in the response. But that is essentially duplicating some the error handling yourself.
Here is a related example of an extract function that sets status to xhr.status (mentioned on Gitter just now): https://mithril.js.org/request.html#retrieving-response-details
@pdfernhout I was trying that and for some reason wasn't getting what I was looking for. I must have been suffering from late-in-the-day brain or something because it's definitely working now. Thanks!
Still, I feel that it's a bit awkward to have to implement custom overriding behaviour just to get this common piece of information.
Right now, I have no good way of retrieving the error code from any errors that the server sends, as an error object is created with the server response and the parsed message with no contextual information.
This issue is related to #1866
Right now, the xhr error code looks like: (https://github.com/MithrilJS/mithril.js/blob/next/request/request.js#L91-L98)
Which throws an unrecognizable error if the response status isn't in the 200 range.
If my web service responds with a
401
with no body (a perfectly valid response), there is no way for me to determine if the error was a401
or some other error (say a500
or something) without modifying what is on the server, which isn't always possible. I also cannot use theextract
method as a workaround as the error throwing works completely aside from theextract
method.For example:
My suggestion:
Modify the https://github.com/MithrilJS/mithril.js/blob/next/request/request.js#L91-L98 to be something like:
Or if going along with #1866:
This has a potential issue if the server body has a key name
code
, but the same can be said about themessage
key that already exists.