core: Vue is not catching errors on server side in SSR when using async child setup
Vue version
3.5.13
Link to minimal reproduction
Steps to reproduce
Just open a link and you will see errors when SSR is ON. When SSR is OFF everything works as expected.
Also, commeting the line with top-level await (making Comp.vue a sync component) will also work fine both with and without SSR.
What is expected?
In SSR mode the page is expected to be blank white for 1 second and then display error message from App.vue.
This works as expected with SSR turned off.
Basically onErrorCaptured should be triggered, child setup and <template> render immediately stopped and error message from parent component shown.
What is actually happening?
In SSR mode the page is blank for 1 second but then it tries to render the <template> of Comp.vue using the data that might not even exist (because it appears in setup after throwing error).
And even if it manages to render <template> correctly, it still does not trigger onErrorCaptured in App.vue and do not switch to show error message.
Any additional comments?
I am building a custom rendering package built on top of Vue and it heavily uses structures like these:
// Wrappers are supposed to catch ALL errors and throws in child <Block /> and show nice error message
<BlockWrapper>
<Block ... />
</BlockWrapper>
<BlockWrapper>
<Block ... />
</BlockWrapper>
<BlockWrapper>
<Block ... />
</BlockWrapper>
...
Because of this bug OR my misunderstanding of how to handle async errors in Vue 3, even when thrown it still tries to render child block’s <template> even with missing data (because setup is stopped executing), throws on reading properties of non-existing objects and the whole Nuxt page breaks.
Please, don’t tell me this is a no-fix. It would be a disaster… 😭
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 33 (16 by maintainers)
Commits related to this issue
- Error Capturing Caveats (#3141) * Update options-lifecycle.md Error capturing caveats. See [issue](https://github.com/vuejs/core/issues/12575#issuecomment-2575368469). * Update src/api/options-... — committed to vuejs/docs by Gwynerva 2 years ago
- Error Capturing Caveats (#3141) * Update options-lifecycle.md Error capturing caveats. See [issue](https://github.com/vuejs/core/issues/12575#issuecomment-2575368469). * Update src/api/options-... — committed to vuejs-translations/docs-pl by Gwynerva 2 years ago
- Error Capturing Caveats (#3141) * Update options-lifecycle.md Error capturing caveats. See [issue](https://github.com/vuejs/core/issues/12575#issuecomment-2575368469). * Update src/api/options-... — committed to vuejs-translations/docs-ja by Gwynerva 2 years ago
- Error Capturing Caveats (#3141) * Update options-lifecycle.md Error capturing caveats. See [issue](https://github.com/vuejs/core/issues/12575#issuecomment-2575368469). * Update src/api/options-... — committed to vuejs-translations/docs-ar by Gwynerva 2 years ago
- Error Capturing Caveats (#3141) * Update options-lifecycle.md Error capturing caveats. See [issue](https://github.com/vuejs/core/issues/12575#issuecomment-2575368469). * Update src/api/options-... — committed to vuejs-translations/docs-fr by Gwynerva 2 years ago
- Merge pull request #126 from monterail/7c4dade2 Error Capturing Caveats * Update options-lifecycle.md Error capturing caveats. See [issue](https://github.com/vuejs/core/issues/12575#issuecommen... — committed to vuejs-translations/docs-pl by Baroshem 2 years ago
- Error Capturing Caveats (#3141) * Update options-lifecycle.md Error capturing caveats. See [issue](https://github.com/vuejs/core/issues/12575#issuecomment-2575368469). * Update src/api/options-... — committed to vuejs-translations/docs-pl by Gwynerva 2 years ago
@Gwynerva
this is a known issue, a duplicate of #7506, and will be fixed by #11471. see Playground with PR 11471
Agreed, PR welcome!
The error threw in
setupis correctly be caught: https://github.com/vuejs/core/blob/833f9ea1f70baec0c3c3147595e10c44e7f87503/packages/runtime-core/src/component.ts#L884-L891 Therefore, regardless of whether thesetupis executed successfully or not (whether there is a correctsetupState), the component will be rendered: https://github.com/vuejs/core/blob/833f9ea1f70baec0c3c3147595e10c44e7f87503/packages/server-renderer/src/render.ts#L117-L119If the
setupexecuted failed, means will render component with an emptysetupState.Is this the expected behavior?@edison1105 Hi again. I was able to work out my tricky situation!
Every child now has separate from
.vuefile asyncprepareDatafunction that runs in<Wrapper>and can throw at any moment while executing. Itry/catchthese errors right in<Wrapper>scope showing nice error message. These errors are considered “expected”.If a error happens in
<Child>“setup” or “render” than yes, it breaks the page. But it actually should, because these errors are not considered “expected”. OnceprepareDatais ready in wrapper, the child is not expected to error at all (or handle such errors inside itself).Such approach creates a distinction between expected errors while preparing data and unexpected errors inside child.
Now to business. It seems the approach of “component replacement in parent when error in child” fails not only with SSR, but even on client side with deep Suspense. I don’t know if this is expected, but Vue shows strange error.
Example
In general, “parent->child” replacement fails everywhere except of non-Suspense non-SSR sync components. If it is expected I think it might be a good idea to change Vue docs
onErrorCapturedpage and add warning message for readers no to try perform such replacements when catching deep errors as they will likely fail and cause more errors.Something like:
I think it also worth noting in docs, that in async context even failed
setupwon’t stop Vue trying rendering the<template>soonErrorCapturedwhen using async children is likely to throw more than once (one for failed setup, and another for most possibly accessing undefined setup properties).If you like the idea, I can make a PR myself. Even make a small example. Just let me know of what you think and tell me, where to do it.
I will investigate this and let you know. Thank you! With your PR this issue can be considered solved I guess.
When calling the setup function of a component
Regardless of whether the component is synchronous or asynchronous, the exception thrown in the
setupfunction always comes first, followed by the exception in therenderfunction.@Gwynerva
Oh, I made a mistake
onServePrefetchwill not work on this scenario because there is no patch on SSR. You can try the preview package of #12601 via:The original issue arises because we catch exceptions during client-side rendering execution at https://github.com/vuejs/core/blob/a038505c653a1d0cb1e929257d3479cee83dcceb/packages/runtime-core/src/componentRenderUtils.ts#L94 However, on the server-side, we don’t catch exceptions at https://github.com/vuejs/core/blob/bf3d9a2af41659a743706306fc798b3d215df5af/packages/server-renderer/src/render.ts#L192 I think this should be fixed.
These two examples capture the same error messages. Below are the modified versions, please observe the console.log without Suspense and SSR with Suspense and without SSR
When wrapped inside Suspense, the error message is not displayed because of
error.value = etriggers an update of Suspense, causing theCompnot to resolve. I don’t think this can be considered a bug.Sounds you can do like this, but this is just temporary measures. After this issue is resolved, there should be more humanized approaches available.