Created
January 16, 2019 14:32
-
-
Save vespakoen/656927030f6fbcfa7c41cee1f316c3eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function safeRun(code: string) { | |
const safeCode = `(function () { try { ${code} | |
} catch (err) { | |
function errorToPretty (err) { | |
var stack = $.stack.split('\\n') | |
stack.shift() | |
stack.pop() | |
stack.pop() | |
var lines = (err.source && err.source.split('\\n')) || [] | |
err.line--; | |
return { | |
name: err.name, | |
message: err.message, | |
line: err.line, | |
context: [ | |
lines[err.line - 2] || '', | |
lines[err.line - 1] || '', | |
'---> ' + lines[err.line] || '', | |
lines[err.line + 1] || '', | |
lines[err.line + 2] || '' | |
], | |
stack: stack | |
} | |
} | |
return JSON.stringify(errorToPretty(err)) | |
} | |
})()`; | |
return evalExtendscript(safeCode) | |
.then(res => { | |
if (typeof res === 'object' && res.stack && res.line && res.message && res.context) { | |
const e = new Error(`${res.message}\nCONTEXT:\n${res.context.join('\n')}\nSTACK:\n${res.stack.join('\n')}`) | |
e.stack = null | |
throw e | |
} | |
return res | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment