Skip to content

Commit

Permalink
[FEATURE ds-extended-errors] add DS.ServerError
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Mar 29, 2016
1 parent 768ae7a commit 01a3b0f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ entry in `config/features.json`.
* [403] `DS.ForbiddenError`
* [404] `DS.NotFoundError`
* [409] `DS.ConflictError`
* [500] `DS.ServerError`
7 changes: 7 additions & 0 deletions addon/adapters/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ export const NotFoundError = extendedErrorsEnabled ?
export const ConflictError = extendedErrorsEnabled ?
extend(AdapterError, 'The adapter operation failed due to a conflict') : null;

/**
@class ServerError
@namespace DS
*/
export const ServerError = extendedErrorsEnabled ?
extend(AdapterError, 'The adapter operation failed due to a server error') : null;

/**
@method errorsHashToArray
@private
Expand Down
5 changes: 5 additions & 0 deletions addon/adapters/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ForbiddenError,
NotFoundError,
ConflictError,
ServerError,
TimeoutError,
AbortError
} from 'ember-data/adapters/errors';
Expand Down Expand Up @@ -920,6 +921,10 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
return new NotFoundError(errors, detailedMessage);
case 409:
return new ConflictError(errors, detailedMessage);
default:
if (status >= 500) {
return new ServerError(errors, detailedMessage);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
ForbiddenError,
NotFoundError,
ConflictError,
ServerError,
TimeoutError,
AbortError,
errorsHashToArray,
Expand Down Expand Up @@ -112,6 +113,7 @@ if (isEnabled('ds-extended-errors')) {
DS.ForbiddenError = ForbiddenError;
DS.NotFoundError = NotFoundError;
DS.ConflictError = ConflictError;
DS.ServerError = ServerError;
}

DS.errorsHashToArray = errorsHashToArray;
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,18 @@ if (isEnabled('ds-extended-errors')) {
});
});

Ember.$.ajax = function(hash) {
jqXHR.status = 500;
hash.error(jqXHR, 'error');
};

Ember.run(function() {
store.find('post', '1').then(null, function(reason) {
assert.ok(true, 'promise should be rejected');
assert.ok(reason instanceof DS.ServerError, 'reason should be an instance of DS.ServerError');
});
});

Ember.$.ajax = originalAjax;
});
}
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/adapter-errors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ if (isEnabled('ds-extended-errors')) {
assert.equal(error.message, 'The adapter operation failed due to a conflict');
});

test("DS.ServerError", function(assert) {
var error = new DS.ServerError();
assert.ok(error instanceof Error);
assert.ok(error instanceof DS.AdapterError);
assert.ok(error.isAdapterError);
assert.equal(error.message, 'The adapter operation failed due to a server error');
});

test("CustomAdapterError", function(assert) {
var CustomAdapterError = DS.AdapterError.extend();
var error = new CustomAdapterError();
Expand Down

0 comments on commit 01a3b0f

Please sign in to comment.