Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ignoreResponseError option #221

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ await ofetch('http://google.com/404')
// at async main (/project/playground.ts:4:3)
```

In order to bypass errors as response you can use `error.data`:
To catch error response:

```ts
await ofetch(...).catch((error) => error.data)
await ofetch('/url').catch(err => err.data)
```

To bypass status error catching you can set `ignoreResponseError` option:

```ts
await ofetch('/url', { ignoreResponseError: true })
```

## ✔️ Auto Retry
Expand Down
8 changes: 6 additions & 2 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface FetchOptions<R extends ResponseType = ResponseType>
extends Omit<RequestInit, "body"> {
baseURL?: string;
body?: RequestInit["body"] | Record<string, any>;
ignoreResponseError?: boolean;
params?: SearchParameters;
query?: SearchParameters;
parseResponse?: (responseText: string) => any;
Expand Down Expand Up @@ -203,11 +204,14 @@ export function createFetch(globalOptions: CreateFetchOptions): $Fetch {
await context.options.onResponse(context as any);
}

if (context.response.status >= 400 && context.response.status < 600) {
if (
!context.options.ignoreResponseError &&
context.response.status >= 400 &&
context.response.status < 600
) {
if (context.options.onResponseError) {
await context.options.onResponseError(context as any);
}

return await onError(context);
}

Expand Down
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { listen } from "listhen";
import { getQuery, joinURL } from "ufo";
import {
createApp,
createError,
eventHandler,
readBody,
readRawBody,
Expand Down Expand Up @@ -47,6 +48,12 @@ describe("ofetch", () => {
.use(
"/echo",
eventHandler(async (event) => ({ body: await readRawBody(event) }))
)
.use(
"/403",
eventHandler(() =>
createError({ status: 403, statusMessage: "Forbidden" })
)
);
listener = await listen(toNodeListener(app));
});
Expand Down Expand Up @@ -169,6 +176,12 @@ describe("ofetch", () => {
expect(error.request).to.equal(getURL("404"));
});

it("403 with ignoreResponseError", async () => {
const res = await $fetch(getURL("403"), { ignoreResponseError: true });
expect(res?.statusCode).to.eq(403);
expect(res?.statusMessage).to.eq("Forbidden");
});

it("baseURL with retry", async () => {
const error = await $fetch("", { baseURL: getURL("404"), retry: 3 }).catch(
(error_) => error_
Expand Down