Skip to content

Instantly share code, notes, and snippets.

@ryrych
Created November 26, 2015 18:35
Show Gist options
  • Save ryrych/8c379894300a6ce539e5 to your computer and use it in GitHub Desktop.
Save ryrych/8c379894300a6ce539e5 to your computer and use it in GitHub Desktop.
Before refactoring
```javascript
import { errorize } from '../../../utils/errorize';
import { module, test } from 'qunit';
module('Unit | Utility | errorize');
test('errorize', function(assert) {
var errors = {
responseJSON: {
errors:[
{
code:100,
title:'has already been taken',
detail:'Email has already been taken',
source:{
pointer:'/data/attributes/email',
},
},
{
code:100,
title:'is too short (minimum is 6 characters)',
detail:'Password is too short (minimum is 6 characters)',
source:{
pointer:'/data/attributes/password',
},
},
],
},
};
var expected = {
email: [
{message: 'Email has already been taken'},
],
password: [
{message: 'Password is too short (minimum is 6 characters)'},
],
};
assert.deepEqual(errorize(errors), expected, 'errorize should equal errors');
});
```
```javascript
import Ember from 'ember';
export function errorize(errors) {
var data = {};
errors = errors.responseJSON.errors;
errors.forEach((error)=> {
if (error.source) {
let field = error.source.pointer.split('/').pop().camelize();
if (Ember.isNone(data[field])) {
data[field] = [];
}
data[field].push({message: error.detail});
}
});
return data;
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment