Skip to content

Commit

Permalink
lib: make isStackOverflowError() engine-agnostic
Browse files Browse the repository at this point in the history
Assumption that stack overflow exception has name == "RangeError" is
v8-specific.  Updated logic to dynamically capture error name when
capturing error message.

PR-URL: #19705
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Mike Kaufman authored and Trott committed Apr 2, 2018
1 parent 4d749e1 commit cd5f353
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,25 +572,29 @@ function dnsException(err, syscall, hostname) {
return ex;
}

let MAX_STACK_MESSAGE;
let maxStack_ErrorName;
let maxStack_ErrorMessage;
/**
* Returns true if `err` is a `RangeError` with an engine-specific message.
* Returns true if `err.name` and `err.message` are equal to engine-specific
* values indicating max call stack size has been exceeded.
* "Maximum call stack size exceeded" in V8.
*
* @param {Error} err
* @returns {boolean}
*/
function isStackOverflowError(err) {
if (MAX_STACK_MESSAGE === undefined) {
if (maxStack_ErrorMessage === undefined) {
try {
function overflowStack() { overflowStack(); }
overflowStack();
} catch (err) {
MAX_STACK_MESSAGE = err.message;
maxStack_ErrorMessage = err.message;
maxStack_ErrorName = err.name;
}
}

return err.name === 'RangeError' && err.message === MAX_STACK_MESSAGE;
return err.name === maxStack_ErrorName &&
err.message === maxStack_ErrorMessage;
}

module.exports = exports = {
Expand Down

0 comments on commit cd5f353

Please sign in to comment.