From 6edce00b1a560e149498a1cace7ae96d713922aa Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 12 Oct 2022 10:52:23 +0300 Subject: [PATCH] Inspect: Handle JSON tab crash when the provided object is too big to stringify. (#55939) * fix(inspector): handle json tab crash when too much data * message update when JSON stringify fails due to obj size --- 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 f6361ebb731..313da7021e6 100644 --- a/public/app/features/inspector/InspectJSONTab.tsx +++ b/public/app/features/inspector/InspectJSONTab.tsx @@ -211,5 +211,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; }