diff options
author | Mudit Ameta <zeusdeux@gmail.com> | 2016-01-08 15:55:14 +0530 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-01-11 09:57:15 -0800 |
commit | e2f47f5698c9eff9b160572811e3804482193dfe (patch) | |
tree | 81cd3ca67487f2de9477d49114460bf502b0cda6 /test/parallel/test-util-format.js | |
parent | d157976ceca56662bd7dc512f88af5605a540781 (diff) | |
download | android-node-v8-e2f47f5698c9eff9b160572811e3804482193dfe.tar.gz android-node-v8-e2f47f5698c9eff9b160572811e3804482193dfe.tar.bz2 android-node-v8-e2f47f5698c9eff9b160572811e3804482193dfe.zip |
util: Change how Error objects are formatted
Previously, Error objects were formatted as the result of a `toString`
call bounded by square brackets. They are now formatted as the stack
trace for the given error object. The intention initially was to emulate
how browsers do `console.error` but since that would also impact
`console.warn`, `console.log`, etc, it was decided to make the change at
`util.inspect` level which is in turn used by the `console` package.
Fixes: nodejs#4452
PR-URL: https://github.com/nodejs/node/pull/4582
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-util-format.js')
-rw-r--r-- | test/parallel/test-util-format.js | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 00028ddbb7..92c448578b 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -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'); +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]'); |