cloudflare / cloudflare/worker-sites-template

Unable to Catch Errors?

Open
#44 1 comment 10 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
79
Forks
40
PR merge metrics
No merged PRs in 30d

Description

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._

Contributor guide

Open the contributing guide

Research direction

Start with the fetch-handler pattern shown in the issue and reproduce the behavior using the linked demonstrative repository. Verify the respondWith timing warning and the uncaught errors from handleEvent; done means the template handles those errors without calling respondWith asynchronously.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.