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

Vue SFC Playground

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

Most upvoted comments

@Gwynerva

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

this is a known issue, a duplicate of #7506, and will be fixed by #11471. see Playground with PR 11471

I think it also worth noting in docs, that in async context even failed setup won’t stop Vue trying rendering the <template> so onErrorCaptured when using async children is likely to throw more than once (one for failed setup, and another for most possibly accessing undefined setup properties)

Agreed, PR welcome!

The error threw in setup is correctly be caught: https://github.com/vuejs/core/blob/833f9ea1f70baec0c3c3147595e10c44e7f87503/packages/runtime-core/src/component.ts#L884-L891 Therefore, regardless of whether the setup is executed successfully or not (whether there is a correct setupState), the component will be rendered: https://github.com/vuejs/core/blob/833f9ea1f70baec0c3c3147595e10c44e7f87503/packages/server-renderer/src/render.ts#L117-L119

If the setup executed failed, means will render component with an empty setupState.Is this the expected behavior?

@edison1105 Hi again. I was able to work out my tricky situation!

Every child now has separate from .vue file async prepareData function that runs in <Wrapper> and can throw at any moment while executing. I try/catch these 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”. Once prepareData is 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 onErrorCaptured page 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:

Replacing the errored child component with another will cause hydration mismatches when using SSR and will fail when async child components throw deep within <Suspense>. If you use SSR or <Suspense>, avoid trying replacing errored child components when rendering them. Instead, try to separate logic that can possibly throw from child setup into separate function and execute it in the “wrapper” component’s <script setup> section, where you can safely try/catch the execution process and make replacement if needed before rendering anything.

I think it also worth noting in docs, that in async context even failed setup won’t stop Vue trying rendering the <template> so onErrorCaptured when 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.

@edison1105 Why sync Comp.vue calling both setup and render? Isn’t sync version is supposed to trigger parent onErrorCaptured which causes a component replacement just as it happens in async version? Or why async version is not calling both setup and render?

I want to show only actual error, the error in setup. Am I supposed to show only first error in onErrorCaptured and silently supress all other errors? Because both sync and async children can appear in my error handling wrapper.

Looks totally not safe for me to supress errors…

When calling the setup function of a component

  • if the return value is not a Promise, the component’s render function will continue to execute in the same tick.
  • if the return value is a Promise, it will wait for the Promise to resolve before executing render. In this case, the setup and the Promise resolution do not occur in the same tick. The entire process is as follows:
  1. setup is executed and throws an exception.
  2. error.value is assigned, triggering an update and placing the update function in the queue.
  3. The event loop processes the asynchronous callbacks in the task queue.
  4. update is executed.
  5. The Comp.vue component is unmounted.
  6. After 1s, the Promise resolves, preparing to execute the render function of Comp.vue, but since Comp.vue has already been unmounted, it is no longer processed.

Regardless of whether the component is synchronous or asynchronous, the exception thrown in the setup function always comes first, followed by the exception in the render function.

@Gwynerva

@edison1105 Does not seem to work or I am doing something wrong?

Also, is there a way to use your PR instead of main version of Vue? I tried "vue": "vuejs/core#edison/fix/12575" but that did not work.

Oh, I made a mistake onServePrefetch will not work on this scenario because there is no patch on SSR. You can try the preview package of #12601 via:

npm i https://pkg.pr.new/vue@12601

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.

The example without Suspense and SSR, catch the error in setup and render. The example with Suspense and without SSR, only catch the error in setup, and skip render the component which execute setup failed.

It seems like there are different behaviors in different situations, which one is expected? @edison1105

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 = e triggers an update of Suspense, causing the Comp not to resolve. I don’t think this can be considered a bug.

Unfortunately, the whole point in my library is that wrapper handles the error and adds some handy UI, while child component can be almost anything: KaTeX formula, image, paragraph and etc. Child components are external, often provided by other libraries. And if something goes wrong it is a wrapper that has to catch any errors and display nice error message.

image

Handling errors inside child component eliminates one of main purposes of the wrapper — providing same error design no matter what wrapper is rendering inside. I tried to pass error ref as prop to child and use onErrorCaptured inside child but it fails anyways.

In my situation handling errors in child is a bad practice, because children are supposed “by design” to throw if something goes wrong so wrapper could nicely tell user about a problem in unified way.

Observable block system is a good example of what I am trying to do with Vue.

Sounds you can do like this, but this is just temporary measures. After this issue is resolved, there should be more humanized approaches available.