Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await/then blocks don't allow errors (from promise) to bubble up #5129

Closed
thojanssens opened this issue Jul 11, 2020 · 5 comments · Fixed by #5149
Closed

await/then blocks don't allow errors (from promise) to bubble up #5129

thojanssens opened this issue Jul 11, 2020 · 5 comments · Fixed by #5149

Comments

@thojanssens
Copy link

I have a global error handler for uncaught promise errors, that will send promise errors to my server. This code is in the entry file main.js:

window.addEventListener("unhandledrejection", e => { /* send error to server */ });

Problem is that as soon as I use await/then blocks, the errors can't bubble up:

<script>
  /* some imports */
  let userIdPromise = fetchCurrentUserId() // I tried adding `.catch(e => { throw e });` but error still doesn't bubble up
</script>

{#await userIdPromise}
  <p>...loading</p>
{:then _}
  <App/>
{/await}
@thojanssens thojanssens changed the title await/then blocks don't allow my error (from promise) to bubble up await/then blocks don't allow errors (from promise) to bubble up Jul 11, 2020
@thojanssens
Copy link
Author

what I have to do is:

{#await userIdPromise}
  <p>...loading</p>
{:then _}
  <App/>
{:catch error}
  {bubbleError(error)}
{/await}
function bubbleError(e) { throw e }

Do you think that we can have something nicer to bubble the error up?

@Conduitry
Copy link
Member

I haven't tried this, but if this were as simple as making the error handler here re-reject with the error when there's no catch block, that sounds like a reasonable proposal to me.

@dimfeld
Copy link
Contributor

dimfeld commented Jul 13, 2020

Here's one workaround:

let userIdPromise = fetchCurrentUserId()
userIdPromise.catch(() => { throw e; });

The 2nd line creates a new promise that has the catch handler, but userIdPromise still refers to the original promise, and so the #await handler won't interfere with the one added in the .catch() statement. I put together a proof of concept here: https://svelte.dev/repl/dd8c7d7df1cc426db62d83e21c81daec?version=3.24.0

That said, you would have to do this for every promise so it's not very convenient. I think the approach suggested by @Conduitry is the proper solution.

@irshadshalu
Copy link
Contributor

I have raised #5149 as per @Conduitry 's suggestion.

@Conduitry
Copy link
Member

In 3.25.0, {#await}s with no {:catch} that receive a promise that rejects will now re-emit a rejected promise, which should be able to be caught by an unhandled rejection event listener.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants