0

Currently in the process of upgrading quite a large EmberJS app all the way from 1.7 to 1.13 (and then 2.0 later). We're pretty much there now.

We handle errors from the server differently depending on the HTTP status code. Our current code goes a little like this...

model.save().then(
    // Success
    function() { /* ... */ },
    // Failure
    function(response) {
        switch (response.status) {
            case 403:
                /* ... */ 
            case 422:
                 /* ... */ 
            case 500:
                 /* ... */ 
            default:
                 /* ... */ 

        }
    }
)

Previously, we could just get the status code from the status attribute. Now, that doesn't exist because the response variable is an instance of DS.InvalidError or DS.AdapterError.

Question

How can we get the status code?

Alternatively, how can we distinguish between the different possible response classes (DS.InvalidError and DS.AdapterError)?

Versions

  • Ember: 1.13.6
  • Ember Data: 1.13.7
James Jackson
  • 778
  • 6
  • 12
  • This answer might help you http://stackoverflow.com/questions/31918565/handling-errors-with-the-now-default-ember-data-json-api-adapter/32059528#32059528 – Sarus Sep 04 '15 at 15:28
  • @Sarus I came across that one. We're not using a JSONAPI backend yet and really need access to the original status code. – James Jackson Sep 04 '15 at 15:46
  • Are you using the RESTAdapter? I haven't tried it yet but I think you can still overload the handleResponse method in your adapter to process the status. – Sarus Sep 04 '15 at 16:37

1 Answers1

0

How do you get status code (and if you get it at all) depends on: where error was happen (model hook or not) and what format of the response was (json or text). If your server responds with status and plain text (not object), in your failure callback you will likely receive

{
  errors: [
    {
      detail: "Response from your server",
      status: "500",
      title: "The backend responded with an error"
    }
  ]
}

To handle errors properly I would suggest to create a function ajaxErrors, and call it in case of failure. Inside that function put console.log(arguments); in the first line and then write a code for all cases that you will see during development. It is the only way to cover most of errors. Also, you will need to define an error action in the application route. It will handle errors that will happen during model hooks. Look (end of page) for additional info: http://guides.emberjs.com/v1.13.0/routing/loading-and-error-substates/

Gennady Dogaev
  • 5,902
  • 1
  • 15
  • 23
  • Thanks. I had created a handler for the InvalidError/DS.AdapterError returned in the promise's response on failure. This can handle all the validation error messages returned by the server but not the http status code. An option could be to change the API to return the status inside the JSON object but we're avoiding changes before moving to the JSON-API standard. The error action is what's needed to capture the status code. – James Jackson Sep 11 '15 at 14:07