# How to handle failure to save on server?

**URL:** https://discuss.emberjs.com/t/how-to-handle-failure-to-save-on-server/3789
**Category:** Ember Data
**Created:** [January 9, 2014, 4:11am UTC](https://discuss.emberjs.com/t/how-to-handle-failure-to-save-on-server/3789 "2014-01-09T04:11:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![davetron5000](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/davetron5000/32/15392_2.png) [@davetron5000](https://discuss.emberjs.com/u/davetron5000)
#### Post date: [January 9, 2014, 4:11am UTC](https://discuss.emberjs.com/t/how-to-handle-failure-to-save-on-server/3789/1 "2014-01-09T04:11:04Z")

</div>

Wondering what is the best way to handle a failure to save on the server-side? For example, suppose my Ember app calls `record.save()` and the server is unable to actually do the save, possibly for legit reasons (for example, the record was modified by someone else, or some sort of complex validation wasn’t met).

If my server returns a 4xx, this seems wrong, REST-wise, but is also problematic because the client-side copy of the model is now out of date.

If my server returns a 2xx, how is the client to know that the save failed? If I re-GET, the server returns a 304, leaving the client out of sync again.

When I’ve designed REST APIs before, I’ve typically included an envelope that would allow encapsulating this data, e.g.

```auto
{ 
 success: true,
 record: { the record as normal }
}
// vs.
{
  success: false,
  errorMessage: { "email address is already taken" }
}

```

This format doesn’t seem to play well with Ember Data, so I’m wondering if there’s a more canonical way that Ember Data expects this to work?

---

<div class="post-metadata">

### Author: ![kbiesbrock](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kbiesbrock/32/5216_2.png) [@kbiesbrock](https://discuss.emberjs.com/u/kbiesbrock)
#### Post date: [January 9, 2014, 1:44pm UTC](https://discuss.emberjs.com/t/how-to-handle-failure-to-save-on-server/3789/2 "2014-01-09T13:44:12Z")

</div>

Take a look at [`DS.ActiveModelAdapter.ajaxError`](https://github.com/emberjs/data/blob/v1.0.0-beta.4/packages/activemodel-adapter/lib/system/active_model_adapter.js#L96). The expectation is that your response will have a status of 422 Unprocessable Entity and the payload will be JSON with a top-level object of “errors”, such as:

```
{
    "errors" : {
        "emailAddress" : ["email address is already taken"]
    }
}

```

If you’re not using the ActiveModelAdapter, you can customize your own adapter by doing something like this:

```
App.ApplicationAdapter = DS.RESTAdapter.extend({
    ajaxError: function(jqXHR) {
        // copy the code from DS.ActiveModelAdapter.ajaxError
    }
});

```

The result of `DS.ActiveModelAdapter.ajaxError` ultimately sets the errors to your model so you can access them via `model.errors` and sends your model into an invalid state; however, from `model.errors` you can show which fields are invalid and force the user to fix them before being resubmitted. Attempts to resend the form before fixing the errored fields will throw an error because you cannot save a model in an invalid state.

HTH, Beez
