From fa225291280e161dedca5b923c73f82cda23c3fe Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 14 Dec 2016 14:09:27 -0800 Subject: [PATCH] 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 --- src/fetch-error.js | 6 +++--- test/test.js | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/fetch-error.js b/src/fetch-error.js index a417919..4d4b932 100644 --- a/src/fetch-error.js +++ b/src/fetch-error.js @@ -16,9 +16,6 @@ export default function FetchError(message, type, systemError) { Error.call(this, message); - // hide custom error implementation details from end-users - Error.captureStackTrace(this, this.constructor); - this.message = message; this.type = type; @@ -27,7 +24,10 @@ export default function FetchError(message, type, systemError) { this.code = this.errno = systemError.code; } + // hide custom error implementation details from end-users + Error.captureStackTrace(this, this.constructor); } FetchError.prototype = Object.create(Error.prototype); +FetchError.prototype.constructor = FetchError; FetchError.prototype.name = 'FetchError'; diff --git a/test/test.js b/test/test.js index 1c26044..ae4972b 100644 --- a/test/test.js +++ b/test/test.js @@ -1819,7 +1819,7 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => { expect(body).to.have.property('buffer'); }); - it('should create custom FetchError', function() { + it('should create custom FetchError', function funcName() { const systemError = new Error('system'); systemError.code = 'ESOMEERROR'; @@ -1831,6 +1831,8 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => { 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') + .and.to.startWith(`${err.name}: ${err.message}`); }); it('should support https request', function() {