Improve debug display of error objects

pull/5079/head
Nick O'Leary 2025-03-10 17:43:06 +00:00
parent 7e512c94fe
commit 5eea8b6b60
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 33 additions and 12 deletions

View File

@ -1285,7 +1285,6 @@ RED.utils = (function() {
payload = JSON.parse(payload);
} else if (/error/i.test(format)) {
payload = JSON.parse(payload);
payload = (payload.name?payload.name+": ":"")+payload.message;
} else if (format === 'null') {
payload = null;
} else if (format === 'undefined') {

View File

@ -828,18 +828,25 @@ function encodeObject(msg,opts) {
debuglength = opts.maxLength;
}
var msgType = typeof msg.msg;
if (msg.msg instanceof Error) {
if (msg.msg instanceof Error || /Error/.test(msg.msg?.__proto__?.name)) {
msg.format = "error";
var errorMsg = {};
if (msg.msg.name) {
errorMsg.name = msg.msg.name;
const cause = msg.msg.cause
const value = {
__enc__: true,
type: 'error',
data: {
name: msg.msg.name,
message: msg.msg.message,
cause: cause + "",
stack: msg.msg.stack,
}
if (hasOwnProperty.call(msg.msg, 'message')) {
errorMsg.message = msg.msg.message;
} else {
errorMsg.message = msg.msg.toString();
}
msg.msg = JSON.stringify(errorMsg);
// Remove cause if not defined
if (!cause) {
delete value.data.cause
}
msg.msg = JSON.stringify(value);
} else if (msg.msg instanceof Buffer) {
msg.format = "buffer["+msg.msg.length+"]";
msg.msg = msg.msg.toString('hex');
@ -857,6 +864,7 @@ function encodeObject(msg,opts) {
msg.format = "Object";
}
if (/error/i.test(msg.format)) {
// TODO: check if this is needed
msg.msg = JSON.stringify({
name: msg.msg.name,
message: msg.msg.message
@ -904,8 +912,22 @@ function encodeObject(msg,opts) {
__enc__: true,
type: "internal"
}
} else if (value instanceof Error) {
value = value.toString()
} else if (value instanceof Error || /Error/.test(value?.__proto__?.name)) {
const cause = value.cause
value = {
__enc__: true,
type: 'error',
data: {
name: value.name,
message: value.message,
cause: cause + "",
stack: value.stack,
}
}
// Remove cause if not defined
if (!cause) {
delete value.data.cause
}
} else if (Array.isArray(value) && value.length > debuglength) {
value = {
__enc__: true,