Skip to content

Instantly share code, notes, and snippets.

@tb
Forked from ryrych/errorize.js
Last active November 28, 2015 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tb/fca3046947a7dc516938 to your computer and use it in GitHub Desktop.
Save tb/fca3046947a7dc516938 to your computer and use it in GitHub Desktop.
After refactoring #2 - modified
import Ember from 'ember';
var errorize = Ember.Object.extend({
init() {
this._super();
this.set('errorsList', {});
},
// test should check various inputs and verify addToErrorsList is called with proper arguments
processError(error) {
let field = (/\w+$/.exec(error.source.pointer))[0].camelize();
let message = error.detail;
this.addToErrorsList(field, message);
},
// test should verify adding new field and appending error to existing field
addToErrorsList(field, message) {
(this.get('errorsList')[field] = this.get('errorsList')[field] || [])
.push({message});
},
// test should verify empty and not-empty case
serialize() {
this.get('errors.responseJSON.errors').forEach(this.processError);
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