Skip to content

Commit 0393045

Browse files
BridgeARtargos
authored andcommitted
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). PR-URL: #27243 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent ea62f4a commit 0393045

11 files changed

+76
-9
lines changed

lib/internal/process/execution.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ function createFatalException() {
142142
if (exceptionHandlerState.captureFn !== null) {
143143
exceptionHandlerState.captureFn(er);
144144
} else if (!process.emit('uncaughtException', er, type)) {
145-
// If someone handled it, then great. otherwise, die in C++ land
145+
// If someone handled it, then great. Otherwise, die in C++ land
146146
// since that means that we'll exit the process, emit the 'exit' event.
147+
const { inspect } = require('internal/util/inspect');
148+
const colors = internalBinding('util').guessHandleType(2) === 'TTY' &&
149+
require('internal/tty').hasColors() ||
150+
inspect.defaultOptions.colors;
147151
try {
148152
if (!process._exiting) {
149153
process._exiting = true;
@@ -157,6 +161,7 @@ function createFatalException() {
157161
const { kExpandStackSymbol } = require('internal/util');
158162
if (typeof er[kExpandStackSymbol] === 'function')
159163
er[kExpandStackSymbol]();
164+
er.stack = inspect(er, { colors });
160165
} catch {
161166
// Nothing to be done about it at this point.
162167
}

test/message/assert_throws_stack.out

+17-1
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

+7-1
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

+17-1
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

+1-1
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]

test/message/throw_custom_error.out

+1-1
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

+1-1
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' }

test/message/throw_non_error.out

+1-1
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

+1-1
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
}));

test/pseudo-tty/test-fatal-error.js

+10
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;

test/pseudo-tty/test-fatal-error.out

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*test-fatal-error.js:*
2+
throw err;
3+
^
4+
5+
TypeError: foobar
6+
at Object.<anonymous> (*test-fatal-error.js:*)
7+
*[90m at *(internal*loader.js:*:*)*[39m
8+
*[90m at *(internal*loader.js:*:*)*[39m
9+
*[90m at *(internal*loader.js:*:*)*[39m
10+
*[90m at *(internal*loader.js:*:*)*[39m
11+
*[90m at *[39m
12+
*[90m at *[39m {
13+
bla: *[33mtrue*[39m
14+
}

0 commit comments

Comments
 (0)