events: show inspected error in uncaught 'error' message
If there is no handler for `.emit('error', value)` and `value` is not an `Error` object, we currently just call `.toString()` on it. Almost always, using `util.inspect()` provides better information for diagnostic purposes, so prefer to use that instead. Refs: https://github.com/nodejs/help/issues/1729 PR-URL: https://github.com/nodejs/node/pull/25621 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matheus Marchini <mat@mmarchini.me>
This commit is contained in:
parent
2b65399694
commit
eeea0dd1e7
@ -172,9 +172,18 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
|
|||||||
// up in Node's output if this results in an unhandled exception.
|
// up in Node's output if this results in an unhandled exception.
|
||||||
throw er; // Unhandled 'error' event
|
throw er; // Unhandled 'error' event
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let stringifiedEr;
|
||||||
|
const { inspect } = require('internal/util/inspect');
|
||||||
|
try {
|
||||||
|
stringifiedEr = inspect(er);
|
||||||
|
} catch {
|
||||||
|
stringifiedEr = er;
|
||||||
|
}
|
||||||
|
|
||||||
// At least give some kind of context to the user
|
// At least give some kind of context to the user
|
||||||
const errors = lazyErrors();
|
const errors = lazyErrors();
|
||||||
const err = new errors.ERR_UNHANDLED_ERROR(er);
|
const err = new errors.ERR_UNHANDLED_ERROR(stringifiedEr);
|
||||||
err.context = er;
|
err.context = er;
|
||||||
throw err; // Unhandled 'error' event
|
throw err; // Unhandled 'error' event
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
const EE = new EventEmitter();
|
const EE = new EventEmitter();
|
||||||
|
|
||||||
@ -9,12 +10,24 @@ common.expectsError(
|
|||||||
{
|
{
|
||||||
code: 'ERR_UNHANDLED_ERROR',
|
code: 'ERR_UNHANDLED_ERROR',
|
||||||
type: Error,
|
type: Error,
|
||||||
message: 'Unhandled error. (Accepts a string)'
|
message: "Unhandled error. ('Accepts a string')"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
common.expectsError(
|
common.expectsError(
|
||||||
() => EE.emit('error', { message: 'Error!' }),
|
() => EE.emit('error', { message: 'Error!' }),
|
||||||
|
{
|
||||||
|
code: 'ERR_UNHANDLED_ERROR',
|
||||||
|
type: Error,
|
||||||
|
message: "Unhandled error. ({ message: 'Error!' })"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
common.expectsError(
|
||||||
|
() => EE.emit('error', {
|
||||||
|
message: 'Error!',
|
||||||
|
[util.inspect.custom]() { throw new Error(); }
|
||||||
|
}),
|
||||||
{
|
{
|
||||||
code: 'ERR_UNHANDLED_ERROR',
|
code: 'ERR_UNHANDLED_ERROR',
|
||||||
type: Error,
|
type: Error,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user