S
SolidJS6mo ago
jp

error handling with lazy

I have a simple SSR setup similar to https://github.com/bluwy/create-vite-extra/tree/master/template-ssr-solid. When lazy loading a route's component that has an error such as Invalid JSX: <div> cannot be child of <p>, the dev server crashes due to an unhandled exception, whereas I would expect the ErrorBoundary to catch it or at least be able to catch it when awaiting renderToStringAsync. Checking the source for lazy, I don't see a catch block when evaluating the promise: https://github.com/solidjs/solid/blob/main/packages/solid/src/server/rendering.ts#L470, so it seems the assumption is that lazy loading failures are considered fatal. Perhaps there is another way I'm missing to handle this error?
GitHub
solid/packages/solid/src/server/rendering.ts at main · solidjs/solid
A declarative, efficient, and flexible JavaScript library for building user interfaces. - solidjs/solid
8 Replies
mdynnl
mdynnl6mo ago
this error happens at compile time where you have that incorrect html structure directly in your code whereas ErrorBoundary is meant to catch runtime ones
jp
jpOP6mo ago
that makes sense, but i thought because of the lazy import, it's effectively moved to runtime either way. an app might change routes and lazy load a component that fails to load for whatever reason. if that's not considered recoverable, then it makes sense not to bother handling it. for the dev server case, it's nice to be able to catch everything with no unhandled promise failures. i ended up making a custom lazy that takes a fallback to show it when imports fail, so at least the dev server survives small errors
mdynnl
mdynnl6mo ago
yeah there’s definitely room for improvement there. the dev server shouldn’t crash just because of a compiler error. do note that this only happens when compiling which usually is on dev. the assumption about lazy import seems correct though.
Brendonovich
Brendonovich6mo ago
@jp there is another way to handle this error bc either solid start or vinxi is setup to handle it properly, i'll try find which handler does it
No description
Brendonovich
Brendonovich6mo ago
ah right, start has a custom lazyRoute wrapper around lazy that lets the vite overlay take over if importing fails (which can happen if the module being formatted has a syntax error)
No description
Brendonovich
Brendonovich6mo ago
you can emulate the same behaviour pretty simply
No description
Brendonovich
Brendonovich6mo ago
ironically if you use regular lazy inside solid start you can run into the same problem, so lazy should probably account for this itself
jp
jpOP6mo ago
thanks for looking into it! i did pretty much the same workaround, but provided a fallback node with an error message just to see something in the dom. that might already be enough for now to show a reload button or something of the sort if it did happen on the live site
Want results from more Discord servers?
Add your server