diff --git a/packages/-ember-data/tests/integration/adapter/handle-response-test.js b/packages/-ember-data/tests/integration/adapter/handle-response-test.js index 88feedac345..50ae40bb39d 100644 --- a/packages/-ember-data/tests/integration/adapter/handle-response-test.js +++ b/packages/-ember-data/tests/integration/adapter/handle-response-test.js @@ -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() { @@ -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() { @@ -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() { @@ -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'); + }); }); diff --git a/packages/adapter/addon/rest.js b/packages/adapter/addon/rest.js index 02944d083b5..d61f7a67137 100644 --- a/packages/adapter/addon/rest.js +++ b/packages/adapter/addon/rest.js @@ -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); }