Skip to content

Instantly share code, notes, and snippets.

@ryrych
Created November 27, 2015 20:42
Show Gist options
  • Save ryrych/4a4167f80fc2c46f3151 to your computer and use it in GitHub Desktop.
Save ryrych/4a4167f80fc2c46f3151 to your computer and use it in GitHub Desktop.
After refactoring #2
import Ember from 'ember';
var errorize = Ember.Object.extend({
init() {
this._super();
this.set('errorsList', {});
},
validObject() {
return !!this.get('errors');
},
buildErrorMessage(error) {
let field = this.getErrorField(error);
let message = error.detail;
this.addErrorMessage(field, message);
},
getErrorField(error) {
// { source: { pointer:'/data/attributes/email' } }
return (/\w+$/.exec(error.source.pointer))[0].camelize();
},
addErrorMessage(field, message) {
(this.get('errorsList')[field] = this.get('errorsList')[field] || [])
.push({message});
},
serialize() {
if (!this.validObject()) {
return {};
}
this.get('errors.responseJSON.errors').forEach(error => this.buildErrorMessage(error));
return this.get('errorsList');
},
});
export default errorize;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment