From 59bdca63b637c80e78279fb91379354101f95b31 Mon Sep 17 00:00:00 2001 From: Markus Ekholm Date: Tue, 12 Feb 2019 11:25:26 +0100 Subject: [PATCH] Do not crash if unable to figure out location --- lib/get-loc.js | 44 ++++++++++++++++++++++++-------------------- package.json | 2 +- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/get-loc.js b/lib/get-loc.js index cde5abc..c35acc9 100644 --- a/lib/get-loc.js +++ b/lib/get-loc.js @@ -2,31 +2,35 @@ const path = require("path"); function getLoc(depth) { - let stack, file, frame; + try { + let stack, file, frame; - const pst = Error.prepareStackTrace; - Error.prepareStackTrace = function (_, innerStack) { - Error.prepareStackTrace = pst; - return innerStack; - }; + const pst = Error.prepareStackTrace; + Error.prepareStackTrace = function (_, innerStack) { + Error.prepareStackTrace = pst; + return innerStack; + }; - stack = new Error().stack; - depth = !depth || isNaN(depth) ? 1 : depth > stack.length - 2 ? stack.length - 2 : depth; - stack = stack.slice(depth + 1); + stack = new Error().stack; + depth = !depth || isNaN(depth) ? 1 : depth > stack.length - 2 ? stack.length - 2 : depth; + stack = stack.slice(depth + 1); - do { - frame = stack.shift(); - file = frame && frame.getFileName(); - } while (stack.length && file === "module.js" || file.includes("node_modules")); + do { + frame = stack.shift(); + file = frame && frame.getFileName(); + } while (stack.length && file === "module.js" || file.includes("node_modules")); - let calleePath; - if (process.cwd()) { - calleePath = path.relative(process.cwd(), frame.getFileName()); - } else { - calleePath = frame.getFileName(); - } + let calleePath; + if (process.cwd()) { + calleePath = path.relative(process.cwd(), frame.getFileName()); + } else { + calleePath = frame.getFileName(); + } - return `${calleePath}:${frame.getLineNumber()}`; + return `${calleePath}:${frame.getLineNumber()}`; + } catch (e) { + return undefined; + } } module.exports = { diff --git a/package.json b/package.json index de779ab..7902014 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "Markus Ekholm", "Jens Carlén" ], - "version": "2.1.2", + "version": "2.1.3", "scripts": { "test": "mocha --exit && eslint . --cache && depcheck --ignores=\"prettier\"", "posttest": "eslint --cache .",