#Catch errors generated by components after build.
8 messages · Page 1 of 1 (latest)
Can you wrap the code that might throw an error in a try/catch block?
---
try {
if (Astro.props.t == "x") {
throw new Error("My error")
}
} catch {
console.log("error")
}
---
that would work, yes, but my point is: if a component throws an error, shouldn't the whole page return a 500 error instead of skipping the rendering of the faulty component ?
Ah, I see.
I guess my thought is that if I have a complex page built of like two dozen components, then I wouldn't want the whole page to return 500 if just one small component fails for some reason. I'd want there to be a fall back of some sort that renders instead, so all the other content that is working can still be rendered for users.
that also makes sense, but in case you want a "all or nothing" setup, how would this be achievable ?
there is no way to have a try/catch block at component level
Maybe you could try emitting some (custom) events? Listen for the failure event, and then render the page the way you want instead? But that would only work client side, not server side I guess.
I just tried out a return new Error() in component, which works by rendering a string containing the error message instead of a component, still not a good way to catch errors
I guess it is possible to set Astro.response.status in the catch at component level tho