ember-ajax icon indicating copy to clipboard operation
ember-ajax copied to clipboard

normalizeErrorResponse should handle JSON responses

Open pgrippi opened this issue 11 years ago • 10 comments

My API is returning a JSON response along with an error code status (not any of the ones that are handled), and normalizeErrorResponse is converting the payload to a string here. The results in the detail of the error object being "[object Object]".

Is this the intended behavior?

pgrippi avatar Nov 08 '15 13:11 pgrippi

It's because of this https://github.com/ember-cli/ember-ajax/blob/master/addon/services/ajax.js#L320. This was inherited from EmberData Adapter.

It can easily be changed, but I'm curios what should happen instead. When it's an object, should I just include the object in details or serialize it to JSON?

taras avatar Nov 12 '15 15:11 taras

I was just running into the same issue and resolved it for our use case by overriding the ajax service with

normalizeErrorResponse(status, headers, payload) {
  if (payload && typeof payload === 'object' && payload.errors) {
    return payload.errors;
  } else {
    return payload;
  }
}

Background: if the error payload doesn't follow the { errors: {...}} convention, I would rather see the raw payload and deal with it on the individual error handler.

cspanring avatar Dec 23 '15 14:12 cspanring

Maybe ember-ajax should switch over to what the ember-data adapters are using now? It looks like Its using a much better approach.

handleResponse(status, headers, payload, requestData) {
    if (this.isSuccess(status, headers, payload)) {
      return payload;
    } else if (this.isInvalid(status, headers, payload)) {
      return new InvalidError(payload.errors);
    }

    let errors          = this.normalizeErrorResponse(status, headers, payload);
    let detailedMessage = this.generatedDetailedMessage(status, headers, payload, requestData);

    return new AdapterError(errors, detailedMessage);
  },

https://github.com/emberjs/data/blob/master/addon/-private/adapters/rest-adapter.js#L755

Kilowhisky avatar Dec 30 '15 17:12 Kilowhisky

In current master, normalizeErrorResponse thankfully no longer converts the payload to a string, but I think there's another problem: https://github.com/ember-cli/ember-ajax/blob/1cfd9427ef2ee3170a06fc54ed2777a5f4d329eb/addon/ajax-request.js#L171-L191

If the error isn't a generic AjaxError (i.e. an InvalidError, ServerError, etc.), the normalizeErrorResponse function is never called. Thus if the payload doesn't have an errors key, there's no way to inspect the response from the server. normalizeErrorResponse ought to always be called prior to returning an error.

the83 avatar Jan 13 '16 16:01 the83

@the83 if you have a chance, could you check out #43 and see if that handles the problem you're running into? That changes ember-ajax to handle request errors in the same way that ember-data does. I totally see what you're saying though, the hook should be called first, and then the errors array that that returns should be passed into the other error types. I'd be happy to update #43 to fix that issue.

alexlafroscia avatar Jan 13 '16 22:01 alexlafroscia

@alexlafroscia that commit you just added should fix the problem. Thanks!

the83 avatar Jan 13 '16 23:01 the83

@the83 awesome :+1:

alexlafroscia avatar Jan 13 '16 23:01 alexlafroscia

My back end is returning a 500 code with the following response:

{
    errorMessage: ".. some description..."
}

In my .catch block, the error I receive looks like this:

{
    "message": "Ajax operation failed",
    "name": "Error",
    "errors": [
        {
            "status": "500",
            "title": "The backend responded with an error",
            "detail": "[object Object]"
        }
    ]
}

It looks like my original response is still being stringified; is this intentional? Is there anything I can do to stop this happening?

AndyPyeSC avatar Feb 01 '16 17:02 AndyPyeSC

@AndyPyeSC can you please try the beta release and let us know if this is still a problem.

taras avatar Feb 01 '16 18:02 taras

@taras Just tried it and it's working, thanks!

AndyPyeSC avatar Feb 02 '16 09:02 AndyPyeSC