Skip to content

Commit

Permalink
feat: error object should be returned onError hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekacru committed Sep 9, 2024
1 parent 458cd61 commit 1987423
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dev": "pnpm -F fetch dev",
"test": "pnpm --filter \"./packages/*\" test",
"bump": "bumpp \"./packages/*/package.json\"",
"release": "pnpm build && pnpm bump && pnpm publish --access public",
"typecheck": "pnpm -r typecheck",
"lint": "biome check .",
"format": "biome check . --apply"
Expand Down
12 changes: 8 additions & 4 deletions packages/better-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ export const betterFetch = async <
error: null,
} as any;
}
const parser = options?.jsonParser ?? jsonParse;
const text = await response.text();
const errorObject = isJSONParsable(text) ? await parser(text) : {};
/**
* Error Branch
*/
const errorContext = {
response,
request: context,
error: {
...errorObject,
status: response.status,
statusText: response.statusText,
},
};
for (const onError of hooks.onError) {
if (onError) {
Expand Down Expand Up @@ -188,10 +196,6 @@ export const betterFetch = async <
}
}

const parser = options?.jsonParser ?? jsonParse;
const text = await response.text();
const errorObject = isJSONParsable(text) ? await parser(text) : {};

if (options?.throw) {
throw new BetterFetchError(
response.status,
Expand Down
2 changes: 2 additions & 0 deletions packages/better-fetch/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ZodSchema } from "zod";
import { Schema } from "./create-fetch";
import type { BetterFetchOption } from "./types";
import { BetterFetchError } from "./error";

export type RequestContext<T extends Record<string, any> = any> = {
url: URL;
Expand All @@ -21,6 +22,7 @@ export type SuccessContext = {
export type ErrorContext = {
response: Response;
request: RequestContext;
error: BetterFetchError & Record<string, any>;
};
export interface FetchHooks {
/**
Expand Down
21 changes: 17 additions & 4 deletions packages/better-fetch/src/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,29 @@ describe("hooks", () => {
const f = createFetch({
baseURL: "http://localhost:4001",
customFetchImpl: async (req, init) => {
return new Response(null, {
status: 500,
});
return new Response(
JSON.stringify({
message: "Server Error",
}),
{
status: 500,
},
);
},
onError,
onResponse,
onSuccess,
});
await f("/ok");
expect(onError).toHaveBeenCalled();
expect(onError).toHaveBeenCalledWith({
request: expect.any(Object),
response: expect.any(Response),
error: {
message: "Server Error",
status: 500,
statusText: "",
},
});
expect(onResponse).toHaveBeenCalled();
expect(onSuccess).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit 1987423

Please sign in to comment.