From 538c3795d8ed8154a6527142ed8647a0f4f7eb68 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Tue, 8 Oct 2019 14:29:03 +0100 Subject: [PATCH] Allow cyclic objects in console logs This fixes a bug in rageshake handling that would throw an error if you log a cyclic object, which can be convenient during development. --- src/rageshake/rageshake.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/rageshake/rageshake.js b/src/rageshake/rageshake.js index c848be6c2b..d61956c925 100644 --- a/src/rageshake/rageshake.js +++ b/src/rageshake/rageshake.js @@ -1,6 +1,7 @@ /* Copyright 2017 OpenMarket Ltd Copyright 2018 New Vector Ltd +Copyright 2019 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -76,8 +77,22 @@ class ConsoleLogger { args = args.map((arg) => { if (arg instanceof Error) { return arg.message + (arg.stack ? `\n${arg.stack}` : ''); - } else if (typeof(arg) === 'object') { - return JSON.stringify(arg); + } else if (typeof (arg) === 'object') { + try { + return JSON.stringify(arg); + } catch (e) { + // In development, it can be useful to log complex cyclic + // objects to the console for inspection. This is fine for + // the console, but default `stringify` can't handle that. + // We workaround this by using a special replacer function + // to only log values of the root object and avoid cycles. + return JSON.stringify(arg, (key, value) => { + if (key && typeof value === "object") { + return ""; + } + return value; + }); + } } else { return arg; }