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



commit 0d1621fb7ba7dea6c576c69bdb38b049b8e7ad68
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]