From ceb06bed61010b9f71b1fc0bf76ba239f5f0b129 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Fri, 18 Feb 2022 16:27:22 +0800 Subject: [PATCH] Fixed serialise() for NaN, Infinity, -Infinity and undefined --- lib/LoggingEvent.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/LoggingEvent.js b/lib/LoggingEvent.js index 60ce832..5732a27 100644 --- a/lib/LoggingEvent.js +++ b/lib/LoggingEvent.js @@ -35,9 +35,20 @@ class LoggingEvent { const logData = this.data.map((e) => { // JSON.stringify(new Error('test')) returns {}, which is not really useful for us. // The following allows us to serialize errors correctly. + // duck-typing for Error object if (e && e.message && e.stack) { e = Object.assign({ message: e.message, stack: e.stack }, e); } + // JSON.stringify({a: parseInt('abc'), b: 1/0, c: -1/0}) returns {a: null, b: null, c: null}. + // The following allows us to serialize to NaN, Infinity and -Infinity correctly. + else if (typeof e === 'number' && (isNaN(e) || !isFinite(e))) { + e = e.toString(); + } + // JSON.stringify([undefined]) returns [null]. + // The following allows us to serialize to undefined correctly. + else if (typeof e === 'undefined') { + e = typeof e; + } return e; }); this.data = logData;