Skip to content

Commit

Permalink
[BUGFIX release] Follow JSON-API error object spec
Browse files Browse the repository at this point in the history
"pointer: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. "/data" for a primary data object, or "/data/attributes/title" for a specific attribute]."

Closes #3557

(cherry picked from commit 33c6744)

Conflicts:
	packages/ember-data/lib/adapters/errors.js
  • Loading branch information
tchak authored and bmac committed Jul 22, 2015
1 parent 82bf13b commit 72c2f91
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ test('handleResponse - returns invalid error if 422 response', function(assert)

var error = adapter.handleResponse(jqXHR.status, {}, json).errors[0];

assert.equal(error.detail, "can't be blank");
assert.equal(error.source.pointer, "data/attributes/name");
equal(error.detail, "can't be blank");
equal(error.source.pointer, "/data/attributes/name");
});

test('handleResponse - returns ajax response if not 422 response', function(assert) {
Expand Down
5 changes: 2 additions & 3 deletions packages/ember-data/lib/adapters/errors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const EmberError = Ember.Error;

const forEach = Ember.ArrayPolyfills.forEach;
const SOURCE_POINTER_REGEXP = /data\/(attributes|relationships)\/(.*)/;
const SOURCE_POINTER_REGEXP = /^\/?data\/(attributes|relationships)\/(.*)/;

import {
keysFunc,
Expand Down Expand Up @@ -129,7 +128,7 @@ export function errorsHashToArray(errors) {
title: 'Invalid Attribute',
detail: messages[i],
source: {
pointer: `data/attributes/${key}`
pointer: `/data/attributes/${key}`
}
});
}
Expand Down
18 changes: 14 additions & 4 deletions packages/ember-data/tests/unit/adapter-errors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ var errorsArray = [
{
title: 'Invalid Attribute',
detail: 'is invalid',
source: { pointer: 'data/attributes/name' }
source: { pointer: '/data/attributes/name' }
},
{
title: 'Invalid Attribute',
detail: 'must be a string',
source: { pointer: 'data/attributes/name' }
source: { pointer: '/data/attributes/name' }
},
{
title: 'Invalid Attribute',
detail: 'must be a number',
source: { pointer: 'data/attributes/age' }
source: { pointer: '/data/attributes/age' }
}
];

Expand All @@ -57,6 +57,16 @@ test("errorsArrayToHash", function() {
deepEqual(result, errorsHash);
});

test("errorsArrayToHash without trailing slash", function() {
var result = DS.errorsArrayToHash([
{
detail: 'error message',
source: { pointer: 'data/attributes/name' }
}
]);
deepEqual(result, { name: ['error message'] });
});

test("DS.InvalidError will normalize errors hash with deprecation", function() {
var error;

Expand All @@ -68,7 +78,7 @@ test("DS.InvalidError will normalize errors hash with deprecation", function() {
{
title: 'Invalid Attribute',
detail: 'is invalid',
source: { pointer: 'data/attributes/name' }
source: { pointer: '/data/attributes/name' }
}
]);
});

0 comments on commit 72c2f91

Please sign in to comment.