From 4cbf11d4b0276ca587ae40a40a1d032a570c3e51 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 6 Apr 2019 19:33:07 +0200 Subject: [PATCH] maths and js props: better error messages & line numbers --- src/client/app/widgets/common/widget.js | 12 ++++++++++-- src/client/app/widgets/utils.js | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/client/app/widgets/common/widget.js b/src/client/app/widgets/common/widget.js index 59b5aac3..77388f9f 100644 --- a/src/client/app/widgets/common/widget.js +++ b/src/client/app/widgets/common/widget.js @@ -594,7 +594,9 @@ class Widget extends EventEmitter { return typeof r != 'string' ? JSON.stringify(r) : r }) - } catch (err) {console.debug('#{} parsing error:\n' + err)} + } catch (err) { + console.log(this.getProp('id') + '.' + propName + ': #{} error:\n' + err) + } try { propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{ @@ -602,9 +604,15 @@ class Widget extends EventEmitter { let r = this.jsparsers[code](mathscope) + if (r === undefined) r = '' + return typeof r !== 'string' ? JSON.stringify(r) : r }) - } catch (err) {console.debug('JS{{}} parsing error:\n' + err)} + } catch (err) { + var stackline = err.stack ? err.stack.match(/>:([0-9]+):[0-9]+/) : '', + line = stackline.length > 1 ? ' at line ' + (parseInt(stackline[1]) - 2) : '' + console.log(this.getProp('id') + '.' + propName + ': JS{{}} error:\n' + err + line) + } for (let k in variables) { var v = typeof variables[k] === 'string' ? variables[k] : JSON.stringify(variables[k]) diff --git a/src/client/app/widgets/utils.js b/src/client/app/widgets/utils.js index 26467ae6..dd85a8a3 100644 --- a/src/client/app/widgets/utils.js +++ b/src/client/app/widgets/utils.js @@ -124,7 +124,10 @@ module.exports = { function evaljs(code) { - sandbox.contentWindow.parsers[code] = sandbox.contentWindow.Function(loopProtect(code)) + sandbox.contentWindow.parsers[code] = sandbox.contentWindow.Function( + loopProtect('"use strict";' + code) + .replace(/;\n(if \(__protect.*break;)\n/g, ';$1') // prevent loop protect from breaking stack linenumber + ) return (context)=>{