Skip to content

Commit

Permalink
proper stack first line for FetchError instances (#215) (#340)
Browse files Browse the repository at this point in the history
* proper stack first line for FetchError instances (#215)

The `.stack` property gets cached in the `captureStackTrace()` call, so
whatever is set as the `name` and `message` at that time will be used
for the first line of the stack trace.

Before this patch, FetchError's stack would just say "Error" as the
first line. Now they correctly display the "${name}: ${message}" of the
error instances.

Test case included.

Signed-off-by: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
TooTallNate authored and bitinn committed Sep 8, 2017
1 parent 3c9b64c commit 7948999
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/fetch-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ module.exports = FetchError;
*/
function FetchError(message, type, systemError) {

// hide custom error implementation details from end-users
Error.captureStackTrace(this, this.constructor);

this.name = this.constructor.name;
this.message = message;
this.type = type;
Expand All @@ -29,6 +26,8 @@ function FetchError(message, type, systemError) {
this.code = this.errno = systemError.code;
}

// hide custom error implementation details from end-users
Error.captureStackTrace(this, this.constructor);
}

require('util').inherits(FetchError, Error);
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ describe('node-fetch', function() {
expect(body).to.have.property('buffer');
});

it('should create custom FetchError', function() {
it('should create custom FetchError', function funcName() {
var systemError = new Error('system');
systemError.code = 'ESOMEERROR';

Expand All @@ -1470,6 +1470,8 @@ describe('node-fetch', function() {
expect(err.type).to.equal('test-error');
expect(err.code).to.equal('ESOMEERROR');
expect(err.errno).to.equal('ESOMEERROR');
expect(err.stack).to.include('funcName');
expect(err.stack.split('\n')[0]).to.equal(err.name + ': ' + err.message);
});

it('should support https request', function() {
Expand Down

0 comments on commit 7948999

Please sign in to comment.