-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: Change how Error objects are formatted #4582
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
'use strict'; | ||
require('../common'); | ||
var assert = require('assert'); | ||
var util = require('util'); | ||
var symbol = Symbol('foo'); | ||
const assert = require('assert'); | ||
const util = require('util'); | ||
const symbol = Symbol('foo'); | ||
|
||
assert.equal(util.format(), ''); | ||
assert.equal(util.format(''), ''); | ||
|
@@ -55,13 +55,26 @@ assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%'); | |
})(); | ||
|
||
// Errors | ||
assert.equal(util.format(new Error('foo')), '[Error: foo]'); | ||
const err = new Error('foo'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you're working in this file, can you change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing this in both test-util-format.js and test-util-inspect.js. |
||
assert.equal(util.format(err), err.stack); | ||
function CustomError(msg) { | ||
Error.call(this); | ||
Object.defineProperty(this, 'message', | ||
{ value: msg, enumerable: false }); | ||
Object.defineProperty(this, 'name', | ||
{ value: 'CustomError', enumerable: false }); | ||
Error.captureStackTrace(this, CustomError); | ||
} | ||
util.inherits(CustomError, Error); | ||
assert.equal(util.format(new CustomError('bar')), '[CustomError: bar]'); | ||
const customError = new CustomError('bar'); | ||
assert.equal(util.format(customError), customError.stack); | ||
// Doesn't capture stack trace | ||
function BadCustomError(msg) { | ||
Error.call(this); | ||
Object.defineProperty(this, 'message', | ||
{ value: msg, enumerable: false }); | ||
Object.defineProperty(this, 'name', | ||
{ value: 'BadCustomError', enumerable: false }); | ||
} | ||
util.inherits(BadCustomError, Error); | ||
assert.equal(util.format(new BadCustomError('foo')), '[BadCustomError: foo]'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added fallback in case the error is some custom error that isn't capturing the stack correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a regression test for that? EDIT: Nevermind, I see BadCustomError covers that.