From 0e603eb3d31adcda7645c5bc58dd7fbe2d8b5701 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Mon, 4 Apr 2016 11:44:54 +0100 Subject: [PATCH] Don't swallow error details in ajax service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue - keep the original `ember-ajax` behaviour rather than returning `false` when we hit an ajax error - we should only be using `normalizeErrorResponse` to normalize our error responses 😉 - ensures our application code has access to the returned status code for ajax errors --- ghost/admin/app/services/ajax.js | 6 ++--- .../tests/integration/services/ajax-test.js | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ghost/admin/app/services/ajax.js b/ghost/admin/app/services/ajax.js index bec8ea5025..90e68ee0d3 100644 --- a/ghost/admin/app/services/ajax.js +++ b/ghost/admin/app/services/ajax.js @@ -24,9 +24,9 @@ export default AjaxService.extend({ normalizeErrorResponse(status, headers, payload) { if (payload && typeof payload === 'object') { - return payload.error || payload.errors || payload.message || false; - } else { - return false; + payload.errors = payload.error || payload.errors || payload.message || undefined; } + + return this._super(status, headers, payload); } }); diff --git a/ghost/admin/tests/integration/services/ajax-test.js b/ghost/admin/tests/integration/services/ajax-test.js index ebef98039b..e3cfad5c71 100644 --- a/ghost/admin/tests/integration/services/ajax-test.js +++ b/ghost/admin/tests/integration/services/ajax-test.js @@ -4,7 +4,7 @@ import { it } from 'ember-mocha'; import Pretender from 'pretender'; -import wait from 'ember-test-helpers/wait'; +import {AjaxError, UnauthorizedError} from 'ember-ajax/errors'; function stubAjaxEndpoint(server, response) { server.get('/test/', function () { @@ -75,7 +75,7 @@ describeModule( }); }); - it('correctly returns default error message if no error text provided', function (done) { + it('returns default error object for non built-in error', function (done) { stubAjaxEndpoint(server, {}); let ajax = this.subject(); @@ -83,8 +83,26 @@ describeModule( ajax.request('/test/').then(() => { expect(false).to.be.true; }).catch((error) => { - let [defaultError] = error.errors; - expect(defaultError.detail).to.equal('Ajax operation failed'); + expect(error).to.be.instanceOf(AjaxError); + done(); + }); + }); + + it('returns known error object for built-in errors', function (done) { + server.get('/test/', function () { + return [ + 401, + {'Content-Type': 'application/json'}, + '' + ]; + }); + + let ajax = this.subject(); + + ajax.request('/test/').then(() => { + expect(false).to.be.true; + }).catch((error) => { + expect(error).to.be.instanceOf(UnauthorizedError); done(); }); });