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

fix: only call error handler if status code is >= 400 #153

Merged
merged 1 commit into from
Oct 27, 2022
Merged
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
12 changes: 7 additions & 5 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export interface FetchOptions<R extends ResponseType = ResponseType> extends Omi
}

export interface $Fetch {
<T = any, R extends ResponseType = 'json'>(request: FetchRequest, opts?: FetchOptions<R>): Promise<MappedType<R, T>>
raw<T = any, R extends ResponseType = 'json'>(request: FetchRequest, opts?: FetchOptions<R>): Promise<FetchResponse<MappedType<R, T>>>
create(defaults: FetchOptions): $Fetch
<T = any, R extends ResponseType = 'json'> (request: FetchRequest, opts?: FetchOptions<R>): Promise<MappedType<R, T>>
raw<T = any, R extends ResponseType = 'json'> (request: FetchRequest, opts?: FetchOptions<R>): Promise<FetchResponse<MappedType<R, T>>>
create (defaults: FetchOptions): $Fetch
}

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Expand Down Expand Up @@ -156,13 +156,15 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
await ctx.options.onResponse(ctx as any)
}

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

return onError(ctx)
}

return ctx.response.ok ? ctx.response : onError(ctx)
return ctx.response
}

const $fetch = function $fetch (request, opts) {
Expand Down