From 480ae5efd654ca4776dbe7bbe7c360910f6167b2 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Mon, 5 Jun 2017 17:13:52 -0700 Subject: [PATCH] Fix crash when script throws non-error object --- lib/sandbox.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/sandbox.js b/lib/sandbox.js index 130bfcd..82149cf 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -243,18 +243,25 @@ function errorToFault(err) { if (!err) return null; - // Trim the stack trace at the self-executing anonymous wrapper function - let stackTrimIndex = err.stack.match(/\s+at\s[^\s]+\s+at\s[^\s]+\s\(vm\.js.+\)/).index; + if (!err.name) + return {code: "script", message: `${err}`}; - return { + let fault = { code: `script.${err.name}`, message: err.message, detail: { name: err.name, - message: err.message, - stack: err.stack.slice(0, stackTrimIndex) + message: err.message } + }; + + if (err.stack) { + // Trim the stack trace at the self-executing anonymous wrapper function + let stackTrimIndex = err.stack.match(/\s+at\s[^\s]+\s+at\s[^\s]+\s\(vm\.js.+\)/).index; + fault.detail.stack = err.stack.slice(0, stackTrimIndex); } + + return fault; }