File
Blob: src/workerd/api/tests/error-in-error-event-test.js
| 1 | // Copyright (c) 2024 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import { strictEqual } from 'assert'; |
| 5 | |
| 6 | export const test = { |
| 7 | async test(_, env) { |
| 8 | const resp = await env.subrequest.fetch('http://example.org'); |
| 9 | strictEqual(await resp.text(), 'ok'); |
| 10 | }, |
| 11 | }; |
| 12 | |
| 13 | export default { |
| 14 | async fetch() { |
| 15 | // Throwing an error in the error event listener should not be catchable directly |
| 16 | // but should cause the IoContext to be aborted with the error. |
| 17 | addEventListener( |
| 18 | 'error', |
| 19 | () => { |
| 20 | // This error is not going to be catchable. The best we can do is to log it. |
| 21 | // Unfortunately, workerd currently does not give us any mechanism to verify |
| 22 | // that it was logged in the test. Let's make sure the response is handled |
| 23 | // correctly at least. |
| 24 | throw new Error('boom (real)'); |
| 25 | }, |
| 26 | { once: true } |
| 27 | ); |
| 28 | queueMicrotask(() => { |
| 29 | throw new Error('boom (unused)'); |
| 30 | }); |
| 31 | await scheduler.wait(10); |
| 32 | return new Response('ok'); |
| 33 | }, |
| 34 | }; |