[gjs/ewlsh/whatwg-console: 8/8] console: Add optimal formatting for Errors




commit 9770831bb23304a67fa3df5323ce048b7caf564f
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Aug 14 16:19:22 2021 -0700

    console: Add optimal formatting for Errors

 modules/esm/console.js | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
---
diff --git a/modules/esm/console.js b/modules/esm/console.js
index 147b71f3..de91db48 100644
--- a/modules/esm/console.js
+++ b/modules/esm/console.js
@@ -76,8 +76,18 @@ function formatGenerically(item) {
 
 /**
  * @param {any} item an item to format
+ * @returns {string}
  */
 function formatOptimally(item) {
+    // Handle optimal error formatting.
+    if (item instanceof Error) {
+        return `${item.toString()}${item.stack ? '\n' : ''}${item.stack
+            ?.split('\n')
+            // Pad each stacktrace line.
+            .map(line => line.padStart(2, ' '))
+            .join('\n')}`;
+    }
+
     // TODO: Consider 'optimal' formatting.
     return JSON.stringify(item, null, 4);
 }
@@ -795,9 +805,8 @@ export class Console {
             .map(a => {
                 if (a === null)
                     return 'null';
-                // TODO: Use a better object printer
                 else if (typeof a === 'object')
-                    return JSON.stringify(a);
+                    return formatOptimally(a);
                 else if (typeof a === 'function')
                     return a.toString();
                 else if (typeof a === 'undefined')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]