Skip to content

Commit 0573c0f

Browse files
BridgeARMylesBorins
authored andcommitted
console: make error handling engine agnostic
Calling write could throw a maximum call stack size error. To make sure this is not specific to a single engine (version), lazily populate the correct error message by producing such a error on demand. PR-URL: #17707 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f18d826 commit 0573c0f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/console.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const kCounts = Symbol('counts');
2828
// Track amount of indentation required via `console.group()`.
2929
const kGroupIndent = Symbol('groupIndent');
3030

31+
let MAX_STACK_MESSAGE;
32+
3133
function Console(stdout, stderr, ignoreErrors = true) {
3234
if (!(this instanceof Console)) {
3335
return new Console(stdout, stderr, ignoreErrors);
@@ -111,9 +113,17 @@ function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
111113

112114
stream.write(string, errorhandler);
113115
} catch (e) {
116+
if (MAX_STACK_MESSAGE === undefined) {
117+
try {
118+
// eslint-disable-next-line no-unused-vars
119+
function a() { a(); }
120+
} catch (err) {
121+
MAX_STACK_MESSAGE = err.message;
122+
}
123+
}
114124
// console is a debugging utility, so it swallowing errors is not desirable
115125
// even in edge cases such as low stack space.
116-
if (e.message === 'Maximum call stack size exceeded')
126+
if (e.message === MAX_STACK_MESSAGE && e.name === 'RangeError')
117127
throw e;
118128
// Sorry, there’s no proper way to pass along the error here.
119129
} finally {

0 commit comments

Comments
 (0)