Skip to content

Commit 7948999

Browse files
TooTallNatebitinn
authored andcommitted
proper stack first line for FetchError instances (node-fetch#215) (node-fetch#340)
* proper stack first line for FetchError instances (node-fetch#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 <[email protected]>
1 parent 3c9b64c commit 7948999

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

lib/fetch-error.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ module.exports = FetchError;
1717
*/
1818
function FetchError(message, type, systemError) {
1919

20-
// hide custom error implementation details from end-users
21-
Error.captureStackTrace(this, this.constructor);
22-
2320
this.name = this.constructor.name;
2421
this.message = message;
2522
this.type = type;
@@ -29,6 +26,8 @@ function FetchError(message, type, systemError) {
2926
this.code = this.errno = systemError.code;
3027
}
3128

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

3433
require('util').inherits(FetchError, Error);

test/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ describe('node-fetch', function() {
14581458
expect(body).to.have.property('buffer');
14591459
});
14601460

1461-
it('should create custom FetchError', function() {
1461+
it('should create custom FetchError', function funcName() {
14621462
var systemError = new Error('system');
14631463
systemError.code = 'ESOMEERROR';
14641464

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

14751477
it('should support https request', function() {

0 commit comments

Comments
 (0)