From 2b958f57241f403e0005b8663290f867683de71d Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Tue, 15 Feb 2022 12:40:34 +0000 Subject: [PATCH] Improve module location parsing when adding hook - fixes #3401 - handles brackets and no brackets in stack line - handles short stack --- .../node_modules/@node-red/util/lib/hooks.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/util/lib/hooks.js b/packages/node_modules/@node-red/util/lib/hooks.js index 6dc1a37c9..d8fb6541c 100644 --- a/packages/node_modules/@node-red/util/lib/hooks.js +++ b/packages/node_modules/@node-red/util/lib/hooks.js @@ -67,8 +67,25 @@ function add(hookId, callback) { throw new Error("Hook "+hookId+" already registered") } // Get location of calling code + let callModule; const stack = new Error().stack; - const callModule = stack.split("\n")[2].split("(")[1].slice(0,-1); + const stackEntries = stack.split("\n").slice(1);//drop 1st line (error message) + const stackEntry2 = stackEntries[1];//get 2nd stack entry + if (stackEntry2) { + try { + if (stackEntry2.indexOf(" (") >= 0) { + callModule = stackEntry2.split("(")[1].slice(0, -1); + } else { + callModule = stackEntry2.split(" ").slice(-1)[0]; + } + } catch (error) { + Log.debug(`Unable to determined module when adding hook '${hookId}'. Stack:\n${stackEntries.join("\n")}`); + callModule = "unknown:0:0"; + } + } else { + Log.debug(`Unable to determined module when adding hook '${hookId}'. Stack:\n${stackEntries.join("\n")}`); + callModule = "unknown:0:0"; + } Log.debug(`Adding hook '${hookId}' from ${callModule}`); const hookItem = {cb:callback, location: callModule, previousHook: null, nextHook: null }