Skip to content

Commit

Permalink
fix: only call error handler if status code is >= 400 (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Oct 27, 2022
1 parent 63232f5 commit 385f7fe
Showing 1 changed file with 7 additions and 5 deletions.
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

0 comments on commit 385f7fe

Please sign in to comment.