Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE ds-extended-errors] add DS.ServerError #4287

Merged
merged 1 commit into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 13 additions & 1 deletion tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2304,7 +2304,7 @@ test('on error appends errorThrown for sanity', function(assert) {

if (isEnabled('ds-extended-errors')) {
test("rejects promise with a specialized subclass of DS.AdapterError if ajax responds with http error codes", function(assert) {
assert.expect(8);
assert.expect(10);

var originalAjax = Ember.$.ajax;
var jqXHR = {
Expand Down 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