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>
This commit is contained in:
Sam Hulick 2023-08-01 10:33:38 -05:00 committed by GitHub
parent fb833214e4
commit 5c5da8d2fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Add logging for when JSON.parse fails within hydrate func

View file

@ -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'),
});