Sapper: Error thrown from async routes not being handled

Created on 20 Oct 2018  路  4Comments  路  Source: sveltejs/sapper

Starting from the sapper-template, suppose I change blog/index.json.js to the following:

export async function get(req, res) {
  throw new Error('OOPPPS');
}

Expected Behavior

  1. the error is caught, logged properly and indicated via a 500
  2. when rendering the route /blog server side a status code >= 400 triggers the appropriate error message to be rendered
  3. when prefetching or rendering the blog client-side the error page indicates a problem

Actual Behavior

An unhandled promise rejection error is being logged and the error is silently discarted:

(node:14150) UnhandledPromiseRejectionWarning: Unhandled promise rejection

Serving the request hangs forever (server-side rendering simply hangs).

Environment

[email protected]

bug

Most helpful comment

for me, this:

{#await promise}
  <Loader />
{:then users }
   [...]
{:catch error}
  <ErrorAlert {error} />
{/await}

<script>
import api from '~/lib/api'
import ErrorAlert from '~/components/ErrorAlert.svelte'
import Loader from '~/components/global/Loader.svelte'

let promise

promise = fetch()

async function fetch () {
  return (await api.get(`/users`)).data
}
</script>

is throwing:

(node:27201) UnhandledPromiseRejectionWarning: Error: Request failed with status code 401
    at createError (/home/forge/code/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/home/forge/code/node_modules/axios/lib/core/settle.js:18:12)
    at IncomingMessage.handleStreamEnd (/home/forge/code/node_modules/axios/lib/adapters/http.js:202:11)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:27201) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:27201) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

when running yarn run export

but I can stop it by adding:

// does nothing, but stops UnhandledPromiseRejectionWarning in SSR
$: promise.catch(err => null)

even though I am expecting the {:catch error} to have caught it.

All 4 comments

Closing, as this was fixed in #488

As mentioned here the issue is not 100% resolved. Should I open another issue suggesting follow up improvements?

Still remaining to be fixed:

__Expected Behavior__

  1. when rendering the route /blog server side a status code >= 400 triggers the appropriate error message to be rendered
  2. when prefetching or rendering the blog client-side the error page indicates a problem

Ah, my bad. Yes, if you could that'd be very helpful, thanks

for me, this:

{#await promise}
  <Loader />
{:then users }
   [...]
{:catch error}
  <ErrorAlert {error} />
{/await}

<script>
import api from '~/lib/api'
import ErrorAlert from '~/components/ErrorAlert.svelte'
import Loader from '~/components/global/Loader.svelte'

let promise

promise = fetch()

async function fetch () {
  return (await api.get(`/users`)).data
}
</script>

is throwing:

(node:27201) UnhandledPromiseRejectionWarning: Error: Request failed with status code 401
    at createError (/home/forge/code/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/home/forge/code/node_modules/axios/lib/core/settle.js:18:12)
    at IncomingMessage.handleStreamEnd (/home/forge/code/node_modules/axios/lib/adapters/http.js:202:11)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:27201) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:27201) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

when running yarn run export

but I can stop it by adding:

// does nothing, but stops UnhandledPromiseRejectionWarning in SSR
$: promise.catch(err => null)

even though I am expecting the {:catch error} to have caught it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

UnwrittenFun picture UnwrittenFun  路  4Comments

BMorearty picture BMorearty  路  3Comments

Rich-Harris picture Rich-Harris  路  4Comments

benmccann picture benmccann  路  3Comments

milosdjakovic picture milosdjakovic  路  3Comments