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:
Kevin Ansfield 2016-04-04 11:44:54 +01:00
parent 65d7257ea5
commit 0e603eb3d3
2 changed files with 25 additions and 7 deletions

View File

@ -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);
}
});

View File

@ -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();
});
});