Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

[breaking] Sane handling of 204 response (successful)#1

Open
aars wants to merge 1 commit intopixelhandler:masterfrom
realxsnl:204-is-success
Open

[breaking] Sane handling of 204 response (successful)#1
aars wants to merge 1 commit intopixelhandler:masterfrom
realxsnl:204-is-success

Conversation

@aars
Copy link

@aars aars commented Mar 9, 2017

This is a breaking change, since it modifies the return value of the success handler.

As far as I can tell only ember-jsonapi-resources uses this library, which is also why I believe these changes are necessary:

204 is a successful response. There is no reason to handle it differently, except for the integrated deserialisation and caching in this library.
Because the NoContentHandler resolves with nothing but an empty string, there is no way for a user of this library to know if this was a 204 response (a check for an empty string payload is no good).
EJR in turn is unable to handle a perfectly valid 204 response, except for maybe like so:

this.fetch(url, options).then(payload => { 
  if (!payload) { // do what the normal successPath would have done } 
})

Which is very fragile. Not having a payload does not constitute a 204 (even though a successful response without any content is most likely a 204)

I think it would be good to always resolve with most, if not all useful data about the request, and not hide anything from the user (or, EJR) in such a low-level library as this.

@pixelhandler
Copy link
Owner

@andrewmp1 any thoughts on this change?

this.cacheResponse({
payload: payload, headers: headers, options: options
});
if (payload) {
Copy link
Owner

Choose a reason for hiding this comment

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

@andrewmp1 originally I just used an empty string for 204. But not content means no content. So yeah we can skip the deserialize and cache hooks on 204

@param {Function} resolve - Promise resolve handler
*/
fetchNoContentHandler(response, resolve) {
return response.text().then(function(resp) {
Copy link
Owner

Choose a reason for hiding this comment

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

@andrewmp1 in our apps there was some breakage when the content was empty I had added a string to bypass errors but that was a hack.

// Did we receive a body?
if (text) {
// Parse it as JSON, make it our payload.
let json = JSON.parse(text);
Copy link
Owner

Choose a reason for hiding this comment

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

This may need a try/catch, around JSON.parse()

Copy link
Author

Choose a reason for hiding this comment

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

It looks like this lib is built to only handle JSON, though it's not made clear.

If we do only handle JSON, then nah, no need to catch it, since all we can do it re-throw something that tells the user the same thing; Failed to parse what we expected to be a JSON document. (Or, we could, but only to improve on the error message, while taking a performance hit on every request).

If this library should be able to handle other document types, or simply pass through the payload, then other work needs to be done.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants