Skip to content

Commit f044b21

Browse files
committed
process: inspect error in case of a fatal exception
This makes sure that errors that shut down the application are inspected with `util.inspect()`. That makes sure that all extra properties on the error will be visible and also that the stack trace is highlighted (Node.js internal frames will be grey and node modules are underlined).
1 parent 4416127 commit f044b21

11 files changed

+76
-9
lines changed

β€Žlib/internal/process/execution.jsβ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ function createFatalException() {
138138
if (exceptionHandlerState.captureFn !== null) {
139139
exceptionHandlerState.captureFn(er);
140140
} else if (!process.emit('uncaughtException', er, type)) {
141-
// If someone handled it, then great. otherwise, die in C++ land
141+
// If someone handled it, then great. Otherwise, die in C++ land
142142
// since that means that we'll exit the process, emit the 'exit' event.
143+
const { inspect } = require('internal/util/inspect');
144+
const colors = internalBinding('util').guessHandleType(2) === 'TTY' &&
145+
require('internal/tty').hasColors() ||
146+
inspect.defaultOptions.colors;
143147
try {
144148
if (!process._exiting) {
145149
process._exiting = true;
@@ -153,6 +157,7 @@ function createFatalException() {
153157
const { kExpandStackSymbol } = require('internal/util');
154158
if (typeof er[kExpandStackSymbol] === 'function')
155159
er[kExpandStackSymbol]();
160+
er.stack = inspect(er, { colors });
156161
} catch {
157162
// Nothing to be done about it at this point.
158163
}

β€Žtest/message/assert_throws_stack.outβ€Ž

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,20 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
1515
at *
1616
at *
1717
at *
18-
at *
18+
at * {
19+
generatedMessage: true,
20+
code: 'ERR_ASSERTION',
21+
actual: Error: foo
22+
at assert.throws.bar (*assert_throws_stack.js:*)
23+
at getActual (assert.js:*)
24+
at Function.throws (assert.js:*)
25+
at Object.<anonymous> (*assert_throws_stack.js:*:*)
26+
at *
27+
at *
28+
at *
29+
at *
30+
at *
31+
at *,
32+
expected: [Object],
33+
operator: 'throws'
34+
}

β€Žtest/message/error_exit.outβ€Ž

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
1313
at Module.load (internal/modules/cjs/loader.js:*:*)
1414
at Function.Module._load (internal/modules/cjs/loader.js:*:*)
1515
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
16-
at internal/main/run_main_module.js:*:*
16+
at internal/main/run_main_module.js:*:* {
17+
generatedMessage: true,
18+
code: 'ERR_ASSERTION',
19+
actual: 1,
20+
expected: 2,
21+
operator: 'strictEqual'
22+
}

β€Žtest/message/if-error-has-good-stack.outβ€Ž

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,20 @@ AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
1616
at Module.load (internal/modules/cjs/loader.js:*:*)
1717
at Function.Module._load (internal/modules/cjs/loader.js:*:*)
1818
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
19-
at internal/main/run_main_module.js:*:*
19+
at internal/main/run_main_module.js:*:* {
20+
generatedMessage: false,
21+
code: 'ERR_ASSERTION',
22+
actual: Error: test error
23+
at c (*if-error-has-good-stack.js:*:*)
24+
at b (*if-error-has-good-stack.js:*:*)
25+
at a (*if-error-has-good-stack.js:*:*)
26+
at Object.<anonymous> (*if-error-has-good-stack.js:*:*)
27+
at Module._compile (internal/modules/cjs/loader.js:*:*)
28+
at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*)
29+
at Module.load (internal/modules/cjs/loader.js:*:*)
30+
at Function.Module._load (internal/modules/cjs/loader.js:*:*)
31+
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
32+
at internal/main/run_main_module.js:*:*
33+
expected: null,
34+
operator: 'ifError'
35+
}

β€Žtest/message/stack_overflow.outβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ before
33
JSON.stringify(array);
44
^
55

6-
RangeError: Maximum call stack size exceeded
6+
[RangeError: Maximum call stack size exceeded]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*test*message*throw_custom_error.js:*
22
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
33
^
4-
MyCustomError: This is a custom message
4+
{ name: 'MyCustomError', message: 'This is a custom message' }

β€Žtest/message/throw_in_line_with_tabs.outβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ before
22
*test*message*throw_in_line_with_tabs.js:*
33
throw ({ foo: 'bar' });
44
^
5-
[object Object]
5+
{ foo: 'bar' }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*test*message*throw_non_error.js:*
22
throw ({ foo: 'bar' });
33
^
4-
[object Object]
4+
{ foo: 'bar' }

β€Žtest/parallel/test-error-reporting.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,5 @@ errExec('throws_error6.js', common.mustCall((err, stdout, stderr) => {
7777

7878
// Object that throws in toString() doesn't print garbage
7979
errExec('throws_error7.js', common.mustCall((err, stdout, stderr) => {
80-
assert.ok(/<toString\(\) threw exception/.test(stderr));
80+
assert.ok(/throw {\r?\n\^\r?\n{ toString: \[Function: toString] }\r?\n$/.test(stderr));
8181
}));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
require('../common');
3+
4+
const { inspect } = require('util');
5+
6+
inspect.defaultOptions.colors = true;
7+
8+
const err = new TypeError('foobar');
9+
err.bla = true;
10+
throw err;

0 commit comments

Comments
Β (0)