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

前端 未结 4 1283
后悔当初
后悔当初 2020-12-12 14:13

ember-data.js: https://github.com/emberjs/data/tree/0396411e39df96c8506de3182c81414c1d0eb981

In short, when there is an error, I want to display error messages in th

相关标签:
4条回答
  • 2020-12-12 14:19

    This weekend I was trying to figure the same thing out. Going off what Luke said, I took a closer look at the ember-data source for the latest commit (Dec 11).

    TLDR; to handle ember-data update/create errors, simply define becameError() and becameInvalid(errors) on your DS.Model instance. The cascade triggered by the RESTadapter's AJAX error callback will eventually call these functions you define.

    Example:

    App.Post = DS.Model.extend
      title: DS.attr "string"
      body: DS.attr "string"
    
      becameError: ->
        # handle error case here
        alert 'there was an error!'
    
      becameInvalid: (errors) ->
        # record was invalid
        alert "Record was invalid because: #{errors}"
    

    Here's the full walk through the source:

    In the REST adapter, the AJAX callback error function is given here:

       this.ajax(this.buildURL(root, id), "PUT", {
          data: data,
          context: this,
          success: function(json) {
            Ember.run(this, function(){
              this.didUpdateRecord(store, type, record, json);
            });
          },
          error: function(xhr) {
            this.didError(store, type, record, xhr);
          }
        });
    

    didError is defined here and it in turn calls the store's recordWasInvalid or recordWasError depending on the response:

      didError: function(store, type, record, xhr) {
        if (xhr.status === 422) {
          var data = JSON.parse(xhr.responseText);
          store.recordWasInvalid(record, data['errors']);
        } else {
          store.recordWasError(record);
        }
      },
    

    In turn, store.recordWasInvalid and store.recordWasError (defined here) call the record (a DS.Model)'s handlers. In the invalid case, it passes along error messages from the adapter as an argument.

     recordWasInvalid: function(record, errors) {
        record.adapterDidInvalidate(errors);
      },
    
      recordWasError: function(record) {
        record.adapterDidError();
      },
    

    DS.Model.adapterDidInvalidate and adapterDidError (defined here) simply send('becameInvalid', errors) or send('becameError') which finally leads us to the handlers here:

      didLoad: Ember.K,
      didUpdate: Ember.K,
      didCreate: Ember.K,
      didDelete: Ember.K,
      becameInvalid: Ember.K,
      becameError: Ember.K,
    

    (Ember.K is just a dummy function for returning this. See here)

    So, the conclusion is, you simply need to define functions for becameInvalid and becameError on your model to handle these cases.

    Hope this helps someone else; the docs certainly don't reflect this right now.

    0 讨论(0)
  • 2020-12-12 14:36

    I just ran into such a situation, not sure if this is already explained anywhere.

    I am using:

    Em.VERSION : 1.0.0
    DS.VERSION : "1.0.0-beta.6"
    Ember Validations (dockyard) : Version: 1.0.0.beta.1
    Ember I18n
    

    The model was initially mixedin with Validation mixin.

    App.Order = DS.Model.extend(Ember.Validations.Mixin, {
    .....
    someAttribute : DS.attr('string'),
    /* Client side input validation with ember-validations */
    validations : {
        someAttribute : {
            presence : {
                message : Ember.I18n.t('translations.someAttributeInputError')
            }
        }
    }
    });
    

    In the template, corresponding handlebars is added. (note that ember validations will automatically add errors to model.errors.<attribute> in case of input validations, I will be using same trade-off in server validations as well)

    <p>{{t 'translations.myString'}}<br>
      {{view Ember.TextField valueBinding="attributeName"}}
      {{#if model.errors.attributeName.length}}<small class="error">{{model.errors.attributeName}}</small>{{/if}}
    </p
    

    Now, we will be saving the Order

    App.get('order').save().then(function () {
      //move to next state?
    }, function(xhr){
      var errors = xhr.responseJSON.errors;
      for(var error in errors){ //this loop is for I18n
        errors[error] = Ember.I18n.t(errors[error]);
      }
      controller.get('model').set('errors', errors); //this will overwrite current errors if any
    });
    

    Now if there is some validation error thrown from server, the returned packet being used is

    {"errors":{"attributeName1":"translations.attributeNameEror",
     "another":"translations.anotherError"}}
    
    status : 422
    

    It is important to use status 422

    So this way, your attribute(s) can be validated client side and again on server side.

    Disclaimer : I am not sure if this is the best way!

    0 讨论(0)
  • 2020-12-12 14:37

    DS.RESTAdapter just got a bit more error handling in this commit but we are still not yet at a point where we have a great recommendation for error handling.

    If you are ambitious/crazy enough to put apps in production today with ember-data (as I have been!), it is best to make sure that the likelihood of failures in your API is extremely low. i.e. validate your data client-side.

    Hopefully, we can update this question with a much better answer in the coming months.

    0 讨论(0)
  • 2020-12-12 14:42

    Since there's currently no good solution in stock Ember-Data, I made my own solution by adding an apiErrors property to DS.Model and then in my RestAdapter subclass (I already needed my own) I added error callbacks to the Ajax calls for createRecord and updateRecord that save the errors and put the model in the "invalid" state, which is supposed to mean client-side or server-side validations failed.

    Here's the code snippets:

    This can go in application.js or some other top-level file:

    DS.Model.reopen({
      // Added for better error handling on create/update
      apiErrors: null
    });
    

    This goes in the error callbacks for createRecord and updateRecord in a RestAdapter subclass:

    error: function(xhr, textStatus, err) {
        console.log(xhr.responseText);
        errors = null;
        try {
            errors = JSON.parse(xhr.responseText).errors;
        } catch(e){} //ignore parse error
        if(errors) {
            record.set('apiErrors',errors);
        }
        record.send('becameInvalid');
    }
    
    0 讨论(0)
提交回复
热议问题