fullstack-build / tslog

📝 tslog - Universal Logger for TypeScript and JavaScript
https://tslog.js.org
MIT License
1.35k stars 63 forks source link

Bug: [BUG] Inspect Polyfill Does Not Gracefully Handle Invalid Dates #266

Open Brittank88 opened 1 year ago

Brittank88 commented 1 year ago

Describe the Bug In the case of an invalid date present inside an object that is being logged, the polyfill throws: image

To Reproduce

  1. Create an object containing an invalid date.
  2. Attempt to log the object with tslog.

Expected Behaviour I believe that logging should be able to continue without failing hard on invalid data of any kind; what the browser does is show these sorts of invalid dates in particular as Invalid Date - pretty simple.

Screenshots image

Additional context N/A

Node.js Version v18.17.0

OS incl. Version Windows 10

terehov commented 1 year ago

Can you provide some code so that I can reproduce your example? Thank you

Brittank88 commented 1 year ago

This is the most concise reproduction I can think of:

import { Logger } from "tslog";

(new Logger()).info({ invalidDate: new Date(NaN) });
Brittank88 commented 1 year ago

This is a patch I'm using in the meantime:

diff --git a/node_modules/tslog/dist/cjs/runtime/browser/util.inspect.polyfil.js b/node_modules/tslog/dist/cjs/runtime/browser/util.inspect.polyfil.js
index a18d789..8d772eb 100644
--- a/node_modules/tslog/dist/cjs/runtime/browser/util.inspect.polyfil.js
+++ b/node_modules/tslog/dist/cjs/runtime/browser/util.inspect.polyfil.js
@@ -78,7 +78,7 @@ function isError(e) {
     return isObject(e) && (objectToString(e) === "[object Error]" || e instanceof Error);
 }
 function isDate(d) {
-    return isObject(d) && objectToString(d) === "[object Date]";
+    return isObject(d) && objectToString(d) === "[object Date]" && !isNaN(d.valueOf());
 }
 function objectToString(o) {
     return Object.prototype.toString.call(o);