mirror of
https://github.com/LukeHagar/crossws.git
synced 2025-12-09 12:27:45 +00:00
feat: allow throwing error with .response prop in upgrade (#113)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
10
src/hooks.ts
10
src/hooks.ts
@@ -66,8 +66,12 @@ export class AdapterHookable {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Response) {
|
const errResponse = (error as { response: Response }).response || error;
|
||||||
return { context, endResponse: error };
|
if (errResponse instanceof Response) {
|
||||||
|
return {
|
||||||
|
context,
|
||||||
|
endResponse: errResponse,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -96,6 +100,8 @@ export type UpgradeRequest =
|
|||||||
headers: Headers;
|
headers: Headers;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type UpgradeError = Response | { readonly response: Response };
|
||||||
|
|
||||||
export interface Hooks {
|
export interface Hooks {
|
||||||
/** Upgrading */
|
/** Upgrading */
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -58,11 +58,15 @@ export function createDemo<T extends Adapter<any, any>>(
|
|||||||
},
|
},
|
||||||
upgrade(req) {
|
upgrade(req) {
|
||||||
if (req.url.endsWith("?unauthorized")) {
|
if (req.url.endsWith("?unauthorized")) {
|
||||||
return new Response("unauthorized", {
|
throw {
|
||||||
status: 401,
|
get response() {
|
||||||
statusText: "Unauthorized",
|
return new Response("unauthorized", {
|
||||||
headers: { "x-error": "unauthorized" },
|
status: 401,
|
||||||
});
|
statusText: "Unauthorized",
|
||||||
|
headers: { "x-error": "unauthorized" },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
req.context.test = "1";
|
req.context.test = "1";
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user