Skip to content

Commit 3bb5269

Browse files
LukeHagarpi0
andauthored
feat: allow throwing error with .response prop in upgrade (#113)
Co-authored-by: Pooya Parsa <[email protected]>
1 parent fdba8ed commit 3bb5269

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/hooks.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ export class AdapterHookable {
6666
};
6767
}
6868
} catch (error) {
69-
if (error instanceof Response) {
70-
return { context, endResponse: error };
69+
const errResponse = (error as { response: Response }).response || error;
70+
if (errResponse instanceof Response) {
71+
return {
72+
context,
73+
endResponse: errResponse,
74+
};
7175
}
7276
throw error;
7377
}
@@ -96,6 +100,8 @@ export type UpgradeRequest =
96100
headers: Headers;
97101
};
98102

103+
export type UpgradeError = Response | { readonly response: Response };
104+
99105
export interface Hooks {
100106
/** Upgrading */
101107
/**

test/fixture/_shared.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ export function createDemo<T extends Adapter<any, any>>(
5858
},
5959
upgrade(req) {
6060
if (req.url.endsWith("?unauthorized")) {
61-
return new Response("unauthorized", {
62-
status: 401,
63-
statusText: "Unauthorized",
64-
headers: { "x-error": "unauthorized" },
65-
});
61+
throw {
62+
get response() {
63+
return new Response("unauthorized", {
64+
status: 401,
65+
statusText: "Unauthorized",
66+
headers: { "x-error": "unauthorized" },
67+
});
68+
},
69+
};
6670
}
6771
req.context.test = "1";
6872
return {

0 commit comments

Comments
 (0)