Fixed serialise() for NaN, Infinity, -Infinity and undefined

This commit is contained in:
Lam Wei Li 2022-02-18 16:27:22 +08:00
parent 2f29d0a0f5
commit ceb06bed61
No known key found for this signature in database
GPG Key ID: 90F6ABECF080D7BF

View File

@ -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;