mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-24 23:48:13 -05:00
Don't swallow error details in ajax service
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
This commit is contained in:
parent
65d7257ea5
commit
0e603eb3d3
2 changed files with 25 additions and 7 deletions
|
@ -24,9 +24,9 @@ export default AjaxService.extend({
|
||||||
|
|
||||||
normalizeErrorResponse(status, headers, payload) {
|
normalizeErrorResponse(status, headers, payload) {
|
||||||
if (payload && typeof payload === 'object') {
|
if (payload && typeof payload === 'object') {
|
||||||
return payload.error || payload.errors || payload.message || false;
|
payload.errors = payload.error || payload.errors || payload.message || undefined;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return this._super(status, headers, payload);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
it
|
it
|
||||||
} from 'ember-mocha';
|
} from 'ember-mocha';
|
||||||
import Pretender from 'pretender';
|
import Pretender from 'pretender';
|
||||||
import wait from 'ember-test-helpers/wait';
|
import {AjaxError, UnauthorizedError} from 'ember-ajax/errors';
|
||||||
|
|
||||||
function stubAjaxEndpoint(server, response) {
|
function stubAjaxEndpoint(server, response) {
|
||||||
server.get('/test/', function () {
|
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, {});
|
stubAjaxEndpoint(server, {});
|
||||||
|
|
||||||
let ajax = this.subject();
|
let ajax = this.subject();
|
||||||
|
@ -83,8 +83,26 @@ describeModule(
|
||||||
ajax.request('/test/').then(() => {
|
ajax.request('/test/').then(() => {
|
||||||
expect(false).to.be.true;
|
expect(false).to.be.true;
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
let [defaultError] = error.errors;
|
expect(error).to.be.instanceOf(AjaxError);
|
||||||
expect(defaultError.detail).to.equal('Ajax operation failed');
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue