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

Merge pull request #3447 from Steve-Mcl/fix-hook-module-location

Improve module location parsing (of stack info) when adding hook
This commit is contained in:
Nick O'Leary 2022-02-15 13:18:25 +00:00 committed by GitHub
commit 263e68e677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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 }