Skip to content

Commit

Permalink
feat: add logging for when hydrate's JSON parse fails (#7887)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <[email protected]>
Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
3 people authored Aug 1, 2023
1 parent fb83321 commit 5c5da8d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-starfishes-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add logging for when JSON.parse fails within hydrate func
26 changes: 23 additions & 3 deletions packages/astro/src/runtime/server/astro-island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
});
Expand Down

0 comments on commit 5c5da8d

Please sign in to comment.