From c2ce0e82ff905244a80f81ab0810b6a904ec235a Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 17 Oct 2022 14:10:54 +0100 Subject: [PATCH] Inspect: Handle JSON tab crash when the provided object is too big to stringify. (#55939) (#56770) * fix(inspector): handle json tab crash when too much data * message update when JSON stringify fails due to obj size (cherry picked from commit 6edce00b1a560e149498a1cace7ae96d713922aa) Co-authored-by: Alex --- public/app/features/inspector/InspectJSONTab.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/public/app/features/inspector/InspectJSONTab.tsx b/public/app/features/inspector/InspectJSONTab.tsx index 6ebcd4f146e..5472d8ef8bd 100644 --- a/public/app/features/inspector/InspectJSONTab.tsx +++ b/public/app/features/inspector/InspectJSONTab.tsx @@ -196,5 +196,18 @@ export class InspectJSONTab extends PureComponent { } function getPrettyJSON(obj: any): string { - return JSON.stringify(obj, null, 2); + let r = ''; + try { + r = JSON.stringify(obj, null, 2); + } catch (e) { + if ( + e instanceof Error && + (e.toString().includes('RangeError') || e.toString().includes('allocation size overflow')) + ) { + appEvents.emit(AppEvents.alertError, [e.toString(), 'Cannot display JSON, the object is too big.']); + } else { + appEvents.emit(AppEvents.alertError, [e instanceof Error ? e.toString() : e]); + } + } + return r; }