Skip to content

Commit

Permalink
[BUGFIX] fix jsonapi error handling when not using jquery (#6941)
Browse files Browse the repository at this point in the history
* make the fix

* changes from PR
  • Loading branch information
aclarembeau authored Jan 30, 2020
1 parent 005ff4d commit d6bd5f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module('integration/adapter/handle-response', function(hooks) {
assert.equal(handleResponseCalled, 1, 'handle response is called');
});

test('handleResponse is called on empty string repsonse', async function(assert) {
test('handleResponse is called on empty string response', async function(assert) {
let handleResponseCalled = 0;

this.server.get('/people', function() {
Expand All @@ -122,7 +122,7 @@ module('integration/adapter/handle-response', function(hooks) {
assert.equal(handleResponseCalled, 1, 'handle response is called');
});

test('handleResponse is not called on invalid repsonse', async function(assert) {
test('handleResponse is not called on invalid response', async function(assert) {
let handleResponseCalled = 0;

this.server.get('/people', function() {
Expand All @@ -149,7 +149,7 @@ module('integration/adapter/handle-response', function(hooks) {
assert.equal(handleResponseCalled, 0, 'handle response is not called');
});

test('handleResponse is called on empty string repsonse with 400 status', async function(assert) {
test('handleResponse is called on empty string response with 400 status', async function(assert) {
let handleResponseCalled = 0;

this.server.get('/people', function() {
Expand All @@ -175,4 +175,34 @@ module('integration/adapter/handle-response', function(hooks) {

assert.equal(handleResponseCalled, 1, 'handle response is called');
});

test('handleResponse is called with correct parameters on string response with 422 status', async function(assert) {
let handleResponseCalled = 0;

let errorObject = { errors: {} };

this.server.get('/people', function() {
return [422, { 'Content-Type': 'application/json' }, JSON.stringify(errorObject)];
});

class TestAdapter extends JSONAPIAdapter {
handleResponse(status, headers, payload, requestData) {
handleResponseCalled++;
assert.deepEqual(payload, errorObject, 'payload from handleResponse matches expected error');

return super.handleResponse(status, headers, payload, requestData);
}
}

this.owner.register('adapter:application', TestAdapter);

try {
await this.store.findAll('person');
assert.ok(false, 'promise should reject');
} catch {
assert.ok(true, 'promise rejected');
}

assert.equal(handleResponseCalled, 1, 'handle response is called');
});
});
2 changes: 2 additions & 0 deletions packages/adapter/addon/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1301,11 +1301,13 @@ function fetchSuccessHandler(adapter, payload, response, requestData) {

function fetchErrorHandler(adapter, payload, response, errorThrown, requestData) {
let responseData = fetchResponseData(response);

if (responseData.status === 200 && payload instanceof Error) {
responseData.errorThrown = payload;
payload = responseData.errorThrown.payload;
} else {
responseData.errorThrown = errorThrown;
payload = adapter.parseErrorResponse(payload);
}
return ajaxError(adapter, payload, requestData, responseData);
}
Expand Down

0 comments on commit d6bd5f1

Please sign in to comment.