Skip to content

Commit

Permalink
Ensure API errors have stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
slobak committed Mar 17, 2015
1 parent e3d0251 commit a0d0d67
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 12 deletions.
12 changes: 12 additions & 0 deletions lib/errors/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function AsanaError(message) {
this.message = message;
try {
throw new Error(message);
} catch (e) {
this.stack = e.stack;
}
}

AsanaError.prototype = Object.create(Error.prototype);

module.exports = AsanaError;
4 changes: 2 additions & 2 deletions lib/errors/forbidden.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var util = require('util');
var AsanaError = require('./error');

function Forbidden(value) {
Error.call(this);
this.message = 'Forbidden';
AsanaError.call(this, 'Forbidden');
this.status = 403;
this.value = value;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/errors/invalid_request.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var util = require('util');
var AsanaError = require('./error');

function InvalidRequest(value) {
Error.call(this);
this.message = 'Invalid Request';
AsanaError.call(this, 'Invalid Request');
this.status = 400;
this.value = value;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/errors/no_authorization.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var util = require('util');
var AsanaError = require('./error');

function NoAuthorization(value) {
Error.call(this);
this.message = 'No Authorization';
AsanaError.call(this, 'No Authorization');
this.status = 401;
this.value = value;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/errors/not_found.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var util = require('util');
var AsanaError = require('./error');

function NotFound(value) {
Error.call(this);
this.message = 'Not Found';
AsanaError.call(this, 'Not Found');
this.status = 404;
this.value = value;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/errors/rate_limit_enforced.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var util = require('util');
var AsanaError = require('./error');

function RateLimitEnforced(value) {
/* jshint camelcase:false */
Error.call(this);
this.message = 'Rate Limit Enforced';
AsanaError.call(this, 'Rate Limit Enforced');
this.status = 429;
this.value = value;
this.retryAfterSeconds = value && parseInt(value.retry_after, 10);
Expand Down
4 changes: 2 additions & 2 deletions lib/errors/server_error.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var util = require('util');
var AsanaError = require('./error');

function ServerError(value) {
Error.call(this);
this.message = 'Server Error';
AsanaError.call(this, 'Server Error');
this.status = 500;
this.value = value;
}
Expand Down
4 changes: 4 additions & 0 deletions test/dispatcher_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ describe('Dispatcher', function() {
return res.then(function() {
throw new Error('Should not have reached here');
}, function(passedErr) {
// Compare everything but stack
assert(passedErr.stack);
delete passedErr.stack;
delete err.stack;
return assert.deepEqual(passedErr, err);
});
});
Expand Down

0 comments on commit a0d0d67

Please sign in to comment.