From ae7f1b38a897c22ee65a1d2407af6a9e97fd4c0d Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sat, 7 Feb 2015 19:52:14 +0000 Subject: [PATCH] Add line/col reporting to Function runtime errors --- nodes/core/core/80-function.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/nodes/core/core/80-function.js b/nodes/core/core/80-function.js index 0aafba333..a333341bf 100644 --- a/nodes/core/core/80-function.js +++ b/nodes/core/core/80-function.js @@ -23,7 +23,7 @@ module.exports = function(RED) { RED.nodes.createNode(this,n); this.name = n.name; this.func = n.func; - var functionText = "var results = null; results = (function(msg){"+this.func+"\n})(msg);"; + var functionText = "var results = null; results = (function(msg){\n"+this.func+"\n})(msg);"; this.topic = n.topic; var sandbox = { console:console, @@ -68,10 +68,22 @@ module.exports = function(RED) { this.status({fill:"yellow",shape:"dot",text:""+converted}); } } catch(err) { - this.error(err.toString()); + var errorMessage = err.toString(); + var stack = err.stack.split(/\r?\n/); + if (stack.length > 0) { + var m = /at undefined:(\d+):(\d+)$/.exec(stack[1]); + if (m) { + var line = Number(m[1])-1; + var cha = m[2]; + errorMessage += " (line "+line+", col "+cha+")"; + } + } + this.error(errorMessage); } }); } catch(err) { + // eg SyntaxError - which v8 doesn't include line number information + // so we can't do better than this this.error(err); } }