node/test/parallel/test-event-emitter-errors.js
Anna Henningsen eeea0dd1e7
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>
2019-01-23 16:43:29 +01:00

37 lines
763 B
JavaScript

'use strict';
const common = require('../common');
const EventEmitter = require('events');
const util = require('util');
const EE = new EventEmitter();
common.expectsError(
() => EE.emit('error', 'Accepts a string'),
{
code: 'ERR_UNHANDLED_ERROR',
type: Error,
message: "Unhandled error. ('Accepts a string')"
}
);
common.expectsError(
() => 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',
type: Error,
message: 'Unhandled error. ([object Object])'
}
);