[gjs/ewlsh/workers-api: 2/4] fix: Guard against circular prints




commit c73dc0d06b154e18b6b0324b0fb5bc7d6839eb9c
Author: Evan Welsh <contact evanwelsh com>
Date:   Fri Mar 25 17:28:18 2022 -0700

    fix: Guard against circular prints

 modules/esm/console.js | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
---
diff --git a/modules/esm/console.js b/modules/esm/console.js
index bdbfc69dd..2ba3b3187 100644
--- a/modules/esm/console.js
+++ b/modules/esm/console.js
@@ -43,7 +43,11 @@ function hasFormatSpecifiers(str) {
  * @param {any} item an item to format
  */
 function formatGenerically(item) {
-    return JSON.stringify(item, null, 4);
+    try {
+        return JSON.stringify(item, null, 4);
+    } catch {
+        return `${item}`;
+    }
 }
 
 /**
@@ -51,19 +55,23 @@ function formatGenerically(item) {
  * @returns {string}
  */
 function formatOptimally(item) {
+    try {
     // Handle optimal error formatting.
-    if (item instanceof Error) {
-        return `${item.toString()}${item.stack ? '\n' : ''}${item.stack
+        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: Enhance 'optimal' formatting.
-    // There is a current work on a better object formatter for GJS in
-    // https://gitlab.gnome.org/GNOME/gjs/-/merge_requests/587
-    return JSON.stringify(item, null, 4);
+        // TODO: Enhance 'optimal' formatting.
+        // There is a current work on a better object formatter for GJS in
+        // https://gitlab.gnome.org/GNOME/gjs/-/merge_requests/587
+        return JSON.stringify(item, null, 4);
+    } catch {
+        return `${item}`;
+    }
 }
 
 const propertyAttributes = {


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