1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Improve module location parsing when adding hook

- fixes #3401
- handles brackets and no brackets in stack line
- handles short stack
This commit is contained in:
Steve-Mcl 2022-02-15 12:40:34 +00:00
parent bffb91f196
commit 2b958f5724

View File

@ -67,8 +67,25 @@ function add(hookId, callback) {
throw new Error("Hook "+hookId+" already registered") throw new Error("Hook "+hookId+" already registered")
} }
// Get location of calling code // Get location of calling code
let callModule;
const stack = new Error().stack; 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}`); Log.debug(`Adding hook '${hookId}' from ${callModule}`);
const hookItem = {cb:callback, location: callModule, previousHook: null, nextHook: null } const hookItem = {cb:callback, location: callModule, previousHook: null, nextHook: null }