From 897df4b9b1ccdbba2715e0b2f92645b2567cb013 Mon Sep 17 00:00:00 2001 From: Varadarajan V <109586712+varadarajan-tw@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:48:16 +0530 Subject: [PATCH] Capture and rethrow json.parse and strigify errors from @json directive --- packages/core/src/mapping-kit/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/core/src/mapping-kit/index.ts b/packages/core/src/mapping-kit/index.ts index 35d1e45829..9a0952076b 100644 --- a/packages/core/src/mapping-kit/index.ts +++ b/packages/core/src/mapping-kit/index.ts @@ -260,10 +260,18 @@ registerDirective('@json', (opts, payload) => { const value = resolve(opts.value, payload) if (opts.mode === 'encode') { - return JSON.stringify(value) + try { + return JSON.stringify(value) + } catch (err) { + throw new Error(`@json: failed to stringify value: ${err}`) + } } else if (opts.mode === 'decode') { if (typeof value === 'string') { - return JSON.parse(value) + try { + return JSON.parse(value) + } catch (err) { + throw new Error(`@json: failed to parse value: ${err}`) + } } return value }