mithril.js icon indicating copy to clipboard operation
mithril.js copied to clipboard

do not reject promise on request returning error status

Open Hugo-Trentesaux opened this issue 7 years ago • 2 comments

Description

The current behaviour of the promise is the following :

  • when no extract function is provided, a 400 status code will lead to promise rejection
  • when an extract function is provided, a 400 status code will lead to promise resolution

This behaviour seems counter-intuitive. I think it should be changed.

I did a pull request to do what I thought would be the more obvious, but @isiahmeadows told me the reason it was like this : implemented in https://github.com/MithrilJS/mithril.js/pull/2006 to comply with https://github.com/MithrilJS/mithril.js/issues/2000

Because these reasons seems reasonable, I think we should align the behaviour of the function when no extract function is provided. That means:

  • if an error status code is returned, this will lead to promise resolution in any case (extract function provided or not)
  • promise rejection will only occur if an error is thrown (unexpected behavior)

Implementation

https://github.com/MithrilJS/mithril.js/blob/master/request/request.js#L115-L138

try {
	var response = xhr.responseText
	if (typeof args.extract === "function") {
		response = args.extract(xhr, args)
	} else if (typeof args.deserialize === "function") {
		response = args.deserialize(response)
	} else {
		try {response = response ? JSON.parse(response) : null}
		catch (e) {throw new Error("Invalid JSON: " + response)}
	}
	resolve(response)
}
catch (e) {
	reject(e)
}

Hugo-Trentesaux avatar Feb 11 '19 18:02 Hugo-Trentesaux

What if the error response is a JSON payload (as it usually is)?

dead-claudia avatar Feb 11 '19 21:02 dead-claudia

If it's valid and no error is raised, it goes through the extract function and then the resolution function.

Hugo-Trentesaux avatar Feb 11 '19 21:02 Hugo-Trentesaux