Catches bad objects being encoded, returning instead the error.

Symptom- Observed that global context would not display in front end, the call returning 400.
Traced to an object in global which cause encodeObject to except.
This push catches that, and now global will display, but the object in question display as an error.
This commit is contained in:
Simon Hailes 2019-11-01 11:38:26 +00:00
parent 6f91786f4d
commit bc283aa025
1 changed files with 128 additions and 113 deletions

View File

@ -652,130 +652,145 @@ function normaliseNodeTypeName(name) {
* @memberof @node-red/util_util * @memberof @node-red/util_util
*/ */
function encodeObject(msg,opts) { function encodeObject(msg,opts) {
var debuglength = 1000; try {
if (opts && opts.hasOwnProperty('maxLength')) { var debuglength = 1000;
debuglength = opts.maxLength; if (opts && opts.hasOwnProperty('maxLength')) {
} debuglength = opts.maxLength;
var msgType = typeof msg.msg;
if (msg.msg instanceof Error) {
msg.format = "error";
var errorMsg = {};
if (msg.msg.name) {
errorMsg.name = msg.msg.name;
} }
if (msg.msg.hasOwnProperty('message')) { var msgType = typeof msg.msg;
errorMsg.message = msg.msg.message; if (msg.msg instanceof Error) {
} else { msg.format = "error";
errorMsg.message = msg.msg.toString(); var errorMsg = {};
} if (msg.msg.name) {
msg.msg = JSON.stringify(errorMsg); errorMsg.name = msg.msg.name;
} else if (msg.msg instanceof Buffer) { }
msg.format = "buffer["+msg.msg.length+"]"; if (msg.msg.hasOwnProperty('message')) {
msg.msg = msg.msg.toString('hex'); errorMsg.message = msg.msg.message;
if (msg.msg.length > debuglength) { } else {
msg.msg = msg.msg.substring(0,debuglength); errorMsg.message = msg.msg.toString();
} }
} else if (msg.msg && msgType === 'object') { msg.msg = JSON.stringify(errorMsg);
try { } else if (msg.msg instanceof Buffer) {
msg.format = msg.msg.constructor.name || "Object"; msg.format = "buffer["+msg.msg.length+"]";
// Handle special case of msg.req/res objects from HTTP In node msg.msg = msg.msg.toString('hex');
if (msg.format === "IncomingMessage" || msg.format === "ServerResponse") { if (msg.msg.length > debuglength) {
msg.msg = msg.msg.substring(0,debuglength);
}
} else if (msg.msg && msgType === 'object') {
try {
msg.format = msg.msg.constructor.name || "Object";
// Handle special case of msg.req/res objects from HTTP In node
if (msg.format === "IncomingMessage" || msg.format === "ServerResponse") {
msg.format = "Object";
}
} catch(err) {
msg.format = "Object"; msg.format = "Object";
} }
} catch(err) { if (/error/i.test(msg.format)) {
msg.format = "Object"; msg.msg = JSON.stringify({
} name: msg.msg.name,
if (/error/i.test(msg.format)) { message: msg.msg.message
msg.msg = JSON.stringify({ });
name: msg.msg.name, } else {
message: msg.msg.message var isArray = util.isArray(msg.msg);
}); if (isArray) {
} else { msg.format = "array["+msg.msg.length+"]";
var isArray = util.isArray(msg.msg); if (msg.msg.length > debuglength) {
if (isArray) { // msg.msg = msg.msg.slice(0,debuglength);
msg.format = "array["+msg.msg.length+"]"; msg.msg = {
if (msg.msg.length > debuglength) {
// msg.msg = msg.msg.slice(0,debuglength);
msg.msg = {
__enc__: true,
type: "array",
data: msg.msg.slice(0,debuglength),
length: msg.msg.length
}
}
}
if (isArray || (msg.format === "Object")) {
msg.msg = safeJSONStringify(msg.msg, function(key, value) {
if (key === '_req' || key === '_res') {
value = {
__enc__: true,
type: "internal"
}
} else if (value instanceof Error) {
value = value.toString()
} else if (util.isArray(value) && value.length > debuglength) {
value = {
__enc__: true, __enc__: true,
type: "array", type: "array",
data: value.slice(0,debuglength), data: msg.msg.slice(0,debuglength),
length: value.length length: msg.msg.length
}
} else if (typeof value === 'string') {
if (value.length > debuglength) {
value = value.substring(0,debuglength)+"...";
}
} else if (typeof value === 'function') {
value = {
__enc__: true,
type: "function"
}
} else if (typeof value === 'number') {
if (isNaN(value) || value === Infinity || value === -Infinity) {
value = {
__enc__: true,
type: "number",
data: value.toString()
}
}
} else if (value && value.constructor) {
if (value.type === "Buffer") {
value.__enc__ = true;
value.length = value.data.length;
if (value.length > debuglength) {
value.data = value.data.slice(0,debuglength);
}
} else if (value.constructor.name === "ServerResponse") {
value = "[internal]"
} else if (value.constructor.name === "Socket") {
value = "[internal]"
} }
} }
return value; }
}," "); if (isArray || (msg.format === "Object")) {
} else { msg.msg = safeJSONStringify(msg.msg, function(key, value) {
try { msg.msg = msg.msg.toString(); } if (key === '_req' || key === '_res') {
catch(e) { msg.msg = "[Type not printable]"; } value = {
__enc__: true,
type: "internal"
}
} else if (value instanceof Error) {
value = value.toString()
} else if (util.isArray(value) && value.length > debuglength) {
value = {
__enc__: true,
type: "array",
data: value.slice(0,debuglength),
length: value.length
}
} else if (typeof value === 'string') {
if (value.length > debuglength) {
value = value.substring(0,debuglength)+"...";
}
} else if (typeof value === 'function') {
value = {
__enc__: true,
type: "function"
}
} else if (typeof value === 'number') {
if (isNaN(value) || value === Infinity || value === -Infinity) {
value = {
__enc__: true,
type: "number",
data: value.toString()
}
}
} else if (value && value.constructor) {
if (value.type === "Buffer") {
value.__enc__ = true;
value.length = value.data.length;
if (value.length > debuglength) {
value.data = value.data.slice(0,debuglength);
}
} else if (value.constructor.name === "ServerResponse") {
value = "[internal]"
} else if (value.constructor.name === "Socket") {
value = "[internal]"
}
}
return value;
}," ");
} else {
try { msg.msg = msg.msg.toString(); }
catch(e) { msg.msg = "[Type not printable]"; }
}
}
} else if (msgType === "function") {
msg.format = "function";
msg.msg = "[function]"
} else if (msgType === "boolean") {
msg.format = "boolean";
msg.msg = msg.msg.toString();
} else if (msgType === "number") {
msg.format = "number";
msg.msg = msg.msg.toString();
} else if (msg.msg === null || msgType === "undefined") {
msg.format = (msg.msg === null)?"null":"undefined";
msg.msg = "(undefined)";
} else {
msg.format = "string["+msg.msg.length+"]";
if (msg.msg.length > debuglength) {
msg.msg = msg.msg.substring(0,debuglength)+"...";
} }
} }
} else if (msgType === "function") { return msg;
msg.format = "function"; } catch(e) {
msg.msg = "[function]" msg.format = "error";
} else if (msgType === "boolean") { var errorMsg = {};
msg.format = "boolean"; if (e.name) {
msg.msg = msg.msg.toString(); errorMsg.name = e.name;
} else if (msgType === "number") {
msg.format = "number";
msg.msg = msg.msg.toString();
} else if (msg.msg === null || msgType === "undefined") {
msg.format = (msg.msg === null)?"null":"undefined";
msg.msg = "(undefined)";
} else {
msg.format = "string["+msg.msg.length+"]";
if (msg.msg.length > debuglength) {
msg.msg = msg.msg.substring(0,debuglength)+"...";
} }
if (e.hasOwnProperty('message')) {
errorMsg.message = e.message;
} else {
errorMsg.message = e.toString();
}
msg.msg = JSON.stringify(errorMsg);
return msg;
} }
return msg;
} }
module.exports = { module.exports = {