Skip to content

Commit

Permalink
feat: allow throwing error with .response prop in upgrade (#113)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
LukeHagar and pi0 authored Jan 24, 2025
1 parent fdba8ed commit 3bb5269
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -96,6 +100,8 @@ export type UpgradeRequest =
headers: Headers;
};

export type UpgradeError = Response | { readonly response: Response };

export interface Hooks {
/** Upgrading */
/**
Expand Down
14 changes: 9 additions & 5 deletions test/fixture/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ export function createDemo<T extends Adapter<any, any>>(
},
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 {
Expand Down

0 comments on commit 3bb5269

Please sign in to comment.