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

Merge pull request #3446 from Steve-Mcl/better-bad-node-err-log

improve "Unexpected Node Error" logging
This commit is contained in:
Nick O'Leary 2022-02-15 11:26:08 +00:00 committed by GitHub
commit 006324b78e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -503,10 +503,25 @@ function log_helper(self, level, msg) {
o.name = self.name; o.name = self.name;
} }
// See https://github.com/node-red/node-red/issues/3327 // See https://github.com/node-red/node-red/issues/3327
// See https://github.com/node-red/node-red/issues/3389
let srcError;
if (msg instanceof Error) {
srcError = msg;//use existing err object for actual stack
} else {
srcError = new Error(msg);//generate a new error for generate a stack
}
try { try {
self._flow.log(o); if(self instanceof Node && self._flow) {
self._flow.log(o);
} else {
//if self._flow is not present, this is not a node-red Node
//Set info to "Node object is not a node-red Node" to point out the `Node type` problem in log
logUnexpectedError(self, srcError, "Node object is not a node-red Node")
}
} catch(err) { } catch(err) {
logUnexpectedError(self, err) //build an unexpected error report indicating using the original error (for better stack trace)
logUnexpectedError(self, srcError, `An error occured attempting to make a log entry: ${err}`)
} }
} }
/** /**
@ -531,7 +546,7 @@ Node.prototype.error = function(logMessage,msg) {
logMessage = logMessage || ""; logMessage = logMessage || "";
} }
var handled = false; var handled = false;
if (msg && typeof msg === 'object') { if (this._flow && msg && typeof msg === 'object') {
handled = this._flow.handleError(this,logMessage,msg); handled = this._flow.handleError(this,logMessage,msg);
} }
if (!handled) { if (!handled) {
@ -619,27 +634,34 @@ function inspectObject(flow) {
} }
} }
function logUnexpectedError(node, error) { function logUnexpectedError(node, error, info) {
let moduleInfo = node._module?`${node._module.module}@${node._module.version}`:"undefined" const header = `
Log.error(`
******************************************************************** ********************************************************************
Unexpected Node Error Unexpected Node Error
${error.stack} ********************************************************************`;
Node:
Type: ${node.type}
Module: ${moduleInfo}
ID: ${node._alias||node.id}
Properties:
${inspectObject(node)}
Flow: ${node._flow?node._flow.path:'undefined'}
Type: ${node._flow?node._flow.TYPE:'undefined'}
Properties:
${node._flow?inspectObject(node._flow):'undefined'}
const footer = `
Please report this issue, including the information logged above: Please report this issue, including the information logged above:
https://github.com/node-red/node-red/issues/ https://github.com/node-red/node-red/issues/
******************************************************************** ********************************************************************`;
`)
let detail = [`Info:\n ${info || 'No additional info'}`];
//Include Error info?
if(error && error.stack){
detail.push(`Stack:\n ${error.stack}`)
}
//Include Node info?
if(node && (node._module || node.type)){
const moduleInfo = node._module?`${node._module.module}@${node._module.version}`:"undefined";
const id = node._alias||node.id||"undefined";
detail.push(`Node:\n Type: ${node.type}\n Module: ${moduleInfo}\n ID: ${id}\n Properties:\n ${inspectObject(node)}`)
}
//Include Flow info?
if(node && node._flow){
detail.push(`Flow: ${node._flow.path}\n Type: ${node._flow.TYPE}\n Properties:\n ${inspectObject(node._flow)}`)
}
Log.error(`${header}\n${detail.join("\n")}\n${footer}`);
} }
module.exports = Node; module.exports = Node;