Skip to content

Unable to Catch Errors? #44

Description

@doublemarked

This template uses the following pattern,

addEventListener('fetch', event => {
  try {
    event.respondWith(handleEvent(event))
  } catch (e) {
      ...
  }
})

async function handleEvent(event) { ... }

However,

  1. event.respondWith is required to be called in the handler without await()ing. Failing to do so yields a warning from Cloudflare (See below)
  2. Because handleEvent is async, it will always return a Promise. It's not possible for the try/catch block to ever catch an error within handleEvent() without making the event handler async and then await()ing on handleEvent (which violates the first point).

Therefore, no exceptions thrown within handleEvent will ever be caught by this boilerplate code.

Here is a demonstrative repository: https://github.com/doublemarked/cf-workers-error-handling


Warning from Cloudflare when trying to asynchronously call event.respondWith:
FetchEvent handler did not call respondWith() before returning, but initiated some asynchronous task. That task will be canceled and default handling will occur -- the request will be sent unmodified to your origin. Remember that you must call respondWith() before the event handler returns, if you don't want default handling. You cannot call it asynchronously later on. If you need to wait for I/O (e.g. a subrequest) before generating a Response, then call respondWith() with a Promise (for the eventual Response) as the argument.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions