diff options
author | 2023-08-01 10:33:38 -0500 | |
---|---|---|
committer | 2023-08-01 16:33:38 +0100 | |
commit | 5c5da8d2fbb37830f3ee81830d4c9afcd2c1a3e3 (patch) | |
tree | 974d82951b14590cbd4c60443d5d49acbd0cf5e4 | |
parent | fb833214e444ce2e52022774f380208fcf75e6b4 (diff) | |
download | astro-5c5da8d2fbb37830f3ee81830d4c9afcd2c1a3e3.tar.gz astro-5c5da8d2fbb37830f3ee81830d4c9afcd2c1a3e3.tar.zst astro-5c5da8d2fbb37830f3ee81830d4c9afcd2c1a3e3.zip |
feat: add logging for when hydrate's JSON parse fails (#7887)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
-rw-r--r-- | .changeset/gorgeous-starfishes-serve.md | 5 | ||||
-rw-r--r-- | packages/astro/src/runtime/server/astro-island.ts | 26 |
2 files changed, 28 insertions, 3 deletions
diff --git a/.changeset/gorgeous-starfishes-serve.md b/.changeset/gorgeous-starfishes-serve.md new file mode 100644 index 000000000..401ab1eed --- /dev/null +++ b/.changeset/gorgeous-starfishes-serve.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Add logging for when JSON.parse fails within hydrate func diff --git a/packages/astro/src/runtime/server/astro-island.ts b/packages/astro/src/runtime/server/astro-island.ts index 5887f3149..a108044ac 100644 --- a/packages/astro/src/runtime/server/astro-island.ts +++ b/packages/astro/src/runtime/server/astro-island.ts @@ -127,9 +127,29 @@ declare const Astro: { if (!closest?.isSameNode(this)) continue; slots[slot.getAttribute('name') || 'default'] = slot.innerHTML; } - const props = this.hasAttribute('props') - ? JSON.parse(this.getAttribute('props')!, reviver) - : {}; + + let props: Record<string, unknown>; + + try { + props = this.hasAttribute('props') + ? JSON.parse(this.getAttribute('props')!, reviver) + : {}; + } catch (e) { + let componentName: string = this.getAttribute('component-url') || '<unknown>'; + const componentExport = this.getAttribute('component-export'); + + if (componentExport) { + componentName += ` (export ${componentExport})`; + } + + // eslint-disable-next-line no-console + console.error( + `[hydrate] Error parsing props for component ${componentName}`, + this.getAttribute('props'), + e + ); + throw e; + } await this.hydrator(this)(this.Component, props, slots, { client: this.getAttribute('client'), }); |