#Catch errors generated by components after build.

8 messages · Page 1 of 1 (latest)

royal turret
#

Hello.
In SSR mode, after build, if a component throws an error under any condition, the page rendering stops with a Internal server error
I made a small setup here: https://stackblitz.com/edit/github-taw4vz

Is there any way to catch such errors to prevent a page to be half rendered ?

Thanks

mint delta
#

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")
}
---
royal turret
#

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 ?

mint delta
#

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.

royal turret
#

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

mint delta
#

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.

royal turret
#

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