Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
decision function and status codes as part of the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
spruce authored and alexlafroscia committed Nov 30, 2017
1 parent 25f4e55 commit 00c84d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
24 changes: 13 additions & 11 deletions addon/errors.js
Expand Up @@ -5,10 +5,11 @@ import EmberError from '@ember/error';
* @public
* @extends Ember.Error
*/
export function AjaxError(payload, message = 'Ajax operation failed') {
export function AjaxError(payload, message = 'Ajax operation failed', status) {
EmberError.call(this, message);

this.payload = payload;
this.status = status;
}

AjaxError.prototype = Object.create(EmberError.prototype);
Expand All @@ -19,7 +20,7 @@ AjaxError.prototype = Object.create(EmberError.prototype);
* @extends AjaxError
*/
export function InvalidError(payload) {
AjaxError.call(this, payload, 'Request was rejected because it was invalid');
AjaxError.call(this, payload, 'Request was rejected because it was invalid', 422);
}

InvalidError.prototype = Object.create(AjaxError.prototype);
Expand All @@ -30,7 +31,7 @@ InvalidError.prototype = Object.create(AjaxError.prototype);
* @extends AjaxError
*/
export function UnauthorizedError(payload) {
AjaxError.call(this, payload, 'Ajax authorization failed');
AjaxError.call(this, payload, 'Ajax authorization failed', 401);
}

UnauthorizedError.prototype = Object.create(AjaxError.prototype);
Expand All @@ -44,7 +45,8 @@ export function ForbiddenError(payload) {
AjaxError.call(
this,
payload,
'Request was rejected because user is not permitted to perform this operation.'
'Request was rejected because user is not permitted to perform this operation.',
403
);
}

Expand All @@ -56,7 +58,7 @@ ForbiddenError.prototype = Object.create(AjaxError.prototype);
* @extends AjaxError
*/
export function BadRequestError(payload) {
AjaxError.call(this, payload, 'Request was formatted incorrectly.');
AjaxError.call(this, payload, 'Request was formatted incorrectly.', 400);
}

BadRequestError.prototype = Object.create(AjaxError.prototype);
Expand All @@ -67,7 +69,7 @@ BadRequestError.prototype = Object.create(AjaxError.prototype);
* @extends AjaxError
*/
export function NotFoundError(payload) {
AjaxError.call(this, payload, 'Resource was not found.');
AjaxError.call(this, payload, 'Resource was not found.', 404);
}

NotFoundError.prototype = Object.create(AjaxError.prototype);
Expand All @@ -78,7 +80,7 @@ NotFoundError.prototype = Object.create(AjaxError.prototype);
* @extends AjaxError
*/
export function TimeoutError() {
AjaxError.call(this, null, 'The ajax operation timed out');
AjaxError.call(this, null, 'The ajax operation timed out', -1);
}

TimeoutError.prototype = Object.create(AjaxError.prototype);
Expand All @@ -89,7 +91,7 @@ TimeoutError.prototype = Object.create(AjaxError.prototype);
* @extends AjaxError
*/
export function AbortError() {
AjaxError.call(this, null, 'The ajax operation was aborted');
AjaxError.call(this, null, 'The ajax operation was aborted', 0);
}

AbortError.prototype = Object.create(AjaxError.prototype);
Expand All @@ -100,7 +102,7 @@ AbortError.prototype = Object.create(AjaxError.prototype);
* @extends AjaxError
*/
export function ConflictError(payload) {
AjaxError.call(this, payload, 'The ajax operation failed due to a conflict');
AjaxError.call(this, payload, 'The ajax operation failed due to a conflict', 409);
}

ConflictError.prototype = Object.create(AjaxError.prototype);
Expand All @@ -110,8 +112,8 @@ ConflictError.prototype = Object.create(AjaxError.prototype);
* @public
* @extends AjaxError
*/
export function ServerError(payload) {
AjaxError.call(this, payload, 'Request was rejected due to server error');
export function ServerError(payload, status) {
AjaxError.call(this, payload, 'Request was rejected due to server error', status);
}

ServerError.prototype = Object.create(AjaxError.prototype);
Expand Down
14 changes: 9 additions & 5 deletions addon/mixins/ajax-request.js
Expand Up @@ -521,15 +521,20 @@ export default Mixin.create({
* @return {Object | AjaxError} response
*/
handleResponse(status, headers, payload, requestData) {
let error;

if (this.isSuccess(status, headers, payload)) {
return payload;
}

// Allow overriding of error payload
payload = this.normalizeErrorResponse(status, headers, payload);

return _createCorrectError(status, headers, payload, requestData);
},

_createCorrectError(status, headers, payload, requestData) {
let error;


if (this.isUnauthorizedError(status, headers, payload)) {
error = new UnauthorizedError(payload);
} else if (this.isForbiddenError(status, headers, payload)) {
Expand All @@ -545,13 +550,12 @@ export default Mixin.create({
} else if (this.isConflictError(status, headers, payload)) {
error = new ConflictError(payload);
} else if (this.isServerError(status, headers, payload)) {
error = new ServerError(payload);
error = new ServerError(payload, status);
} else {
const detailedMessage = this.generateDetailedMessage(status, headers, payload, requestData);

error = new AjaxError(payload, detailedMessage);
error = new AjaxError(payload, detailedMessage, status);
}
error.status = status;

return error;
},
Expand Down

0 comments on commit 00c84d6

Please sign in to comment.