From 43a4aba035a7abd1e1b06469520249ee984a5f52 Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Mon, 16 Sep 2024 16:03:10 +0300 Subject: [PATCH] feat: show text representation when json parsing fails --- index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index bac81d2..518daa2 100644 --- a/index.ts +++ b/index.ts @@ -34,7 +34,12 @@ export class MemoryView extends DataView { // Get the JSON representation of a value stored in Extism memory json(): any { - return JSON.parse(this.text()); + try { + return JSON.parse(this.text()); + } catch (e) { + const text = this.text(); + throw new Error(`Failed to parse JSON: ${text}. Error: ${e.message}`); + } } // Get the string representation of a value stored in Extism memory