From c13c137e24b9fe26f9a6af08dd09714e4f9a0693 Mon Sep 17 00:00:00 2001 From: ruiny Date: Sun, 31 May 2026 16:48:02 +0530 Subject: [PATCH] feat: handle 499 cases as no throw on server * fix(api): gracefully handle aborted requests When a client closes a connection early (e.g. while `await request.text()` is parsing), it throws a `DOMException` with `name: "AbortError"`. Previously, this hit our catch block, was logged as an unhandled exception to the console via `console.error`, and returned a `500` status. This resulted in an alarming error message which looked like a server crash, but the server actually stayed up. This commit updates `errorResponse` in `src/api.ts` to intercept `AbortError` and handle it properly. If we detect an `AbortError`, we now log a simple `400 Bad Request` instead, without writing out the `DOMException` stack trace. * fix(api): gracefully handle aborted requests When a client closes a connection early (e.g. while `await request.text()` is parsing), it throws a `DOMException` with `name: "AbortError"`. Previously, this hit our catch block, was logged as an unhandled exception to the console via `console.error`, and returned a `500` status. This resulted in an alarming error message which looked like a server crash, but the server actually stayed up. This commit updates `errorResponse` in `src/api.ts` to intercept `AbortError` and handle it properly. If we detect an `AbortError`, we now log a simple `499 Client Closed Request` instead, without writing out the `DOMException` stack trace. --------- Co-authored-by: ruinivist <179396038+ruinivist@users.noreply.github.com> --- src/api.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/api.ts b/src/api.ts index 521c204..89428f8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -14,6 +14,10 @@ function errorResponse(error: unknown): Response { return json({ ok: false, error: error.message }, { status: error.status }); } + if (error instanceof Error && error.name === "AbortError") { + return json({ ok: false, error: "Request aborted" }, { status: 499 }); + } + console.error(error); return json({ ok: false, error: "Internal server error" }, { status: 500 }); }