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:
parent
7f92825411
commit
fa22529128
|
@ -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';
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue