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>
This commit is contained in:
Nathan Rajlich 2016-12-14 14:09:27 -08:00 committed by Timothy Gu
parent 7f92825411
commit fa22529128
2 changed files with 6 additions and 4 deletions

View File

@ -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';

View File

@ -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() {