diff --git a/src/hooks.ts b/src/hooks.ts index 69e73e4..58a4a7b 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -66,8 +66,12 @@ export class AdapterHookable { }; } } catch (error) { - if (error instanceof Response) { - return { context, endResponse: error }; + const errResponse = (error as { response: Response }).response || error; + if (errResponse instanceof Response) { + return { + context, + endResponse: errResponse, + }; } throw error; } @@ -96,6 +100,8 @@ export type UpgradeRequest = headers: Headers; }; +export type UpgradeError = Response | { readonly response: Response }; + export interface Hooks { /** Upgrading */ /** diff --git a/test/fixture/_shared.ts b/test/fixture/_shared.ts index 077336d..9afe6c2 100644 --- a/test/fixture/_shared.ts +++ b/test/fixture/_shared.ts @@ -58,11 +58,15 @@ export function createDemo>( }, upgrade(req) { if (req.url.endsWith("?unauthorized")) { - return new Response("unauthorized", { - status: 401, - statusText: "Unauthorized", - headers: { "x-error": "unauthorized" }, - }); + throw { + get response() { + return new Response("unauthorized", { + status: 401, + statusText: "Unauthorized", + headers: { "x-error": "unauthorized" }, + }); + }, + }; } req.context.test = "1"; return {