Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is handling custom server side errors in ember-data when saving model possible

Is there proper way to handle custom error when saving a model? To give an example, lets say I have a model with just two properties "name" and "value". And when I do :

var myModel = this.get('store').createRecord('myModel', {"name": "someName", "value": "someValue"});
myModel.save().then(function() {
    //if success
    //server responded with {"myModel:{"id":1,"name":"someName","value":"someValue"}"}
},function() {
    //if failure
    //server responded with {"error":"some custom error message"}
    //BUT HOW TO CATCH THIS AND POSSIBLY REMOVE THE MODEL FROM THE STORE

});

One way to work around this is to make extra ajax call to check if the name is unique and then do the save. I am just wondering what is the best/elegant approach here.

Thanks, Dee

EDIT : I thought it might help a bit to give more context on the server side of the things in groovy. So here it is:

In my controller I have :

def create() {

    try {
        newRow = someService.create(params)
        render someService.list(newRow) as JSON//returns data in format needed by ember-data
    }
    catch (ValidationException ex) {
        def errors = ["errors":[]]

        ex.errors.allErrors.each{
            if(it.arguments[0] == "fieldName" && it.code=="constrantViolated"){
                errors.errors.push(["field":it.arguments[0],"message":"some custom message"])
            }
        }
        //I am using 422 here because of post in http://stackoverflow.com/questions/7996569/can-we-create-custom-http-status-codes
        render(status: 422, contentType: 'JSON', text: (errors as JSON))
    }

}

Then in my ember controller:

    var myModel = self.get('store').createRecord('myModel ', myModelDataInJSON);
myModel .save().then(function () {
        //if success
                },
    function (response) {
        myModel .deleteRecord();
        var errors = $.parseJSON(response.responseText);
        for (var key in errors.errors) {
            //do something
        }
    });
like image 228
Deewendra Shrestha Avatar asked Oct 24 '13 18:10

Deewendra Shrestha


2 Answers

deleteRecord will delete the record.

myModel.save().then(function(response) {
  //if success
  //server responded with {"myModel:{"id":1,"name":"someName","value":"someValue"}"}
},function(response) {
  //if failure
  //server responded with {"error":"some custom error message"}
  //BUT HOW TO CATCH THIS AND POSSIBLY REMOVE THE MODEL FROM THE STORE
  if(response.error=='no good'){
    myModel.deleteRecord();
  }

});
like image 71
Kingpin2k Avatar answered Nov 10 '22 01:11

Kingpin2k


You can handle errors at model by adding properties into your model:

becameError: ->
  # handle error case here
  alert 'there was an error!'

becameInvalid: (errors) ->
  # record was invalid
  alert "Record was invalid because: #{errors}"

Check: How should errors be handled when using the Ember.js Data RESTAdapter?

like image 2
tjgfernandes Avatar answered Nov 09 '22 23:11

tjgfernandes