Skip to content

Commit

Permalink
🐛 Fix error for opaque responses.
Browse files Browse the repository at this point in the history
Should fix #121
  • Loading branch information
elbywan committed Jan 30, 2022
1 parent 41a0e3d commit bbb5912
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type WretcherResponse = Response & { [key: string]: any }
export type ResponseChain = {
// Response types
res: <Result = WretcherResponse>(cb?: (type: WretcherResponse) => Result) => Promise<Result>,
json: <Result = {[key: string]: any}>(cb?: (type: {[key: string]: any}) => Result) => Promise<Result>,
json: <Result = { [key: string]: any }>(cb?: (type: { [key: string]: any }) => Result) => Promise<Result>,
blob: <Result = Blob>(cb?: (type: Blob) => Result) => Promise<Result>,
formData: <Result = FormData>(cb?: (type: FormData) => Result) => Promise<Result>,
arrayBuffer: <Result = ArrayBuffer>(cb?: (type: ArrayBuffer) => Result) => Promise<Result>,
Expand All @@ -32,7 +32,7 @@ export type ResponseChain = {
}

class WretchErrorWrapper {
constructor(public error: any) {}
constructor(public error: any) { }
}

export const resolver = (wretcher: Wretcher) => {
Expand All @@ -46,14 +46,14 @@ export const resolver = (wretcher: Wretcher) => {
const catchers = new Map(_catchers)
const finalOptions = mix(conf.defaults, opts)
const fetchController = conf.polyfill("AbortController", { doThrow: false, instance: true })
if(!finalOptions["signal"] && fetchController) {
if (!finalOptions["signal"] && fetchController) {
finalOptions["signal"] = fetchController.signal
}
// Request timeout
const timeout = {
ref: null,
clear() {
if(timeout.ref) {
if (timeout.ref) {
clearTimeout(timeout.ref)
timeout.ref = null
}
Expand All @@ -69,6 +69,12 @@ export const resolver = (wretcher: Wretcher) => {
.then(response => {
timeout.clear()
if (!response.ok) {
if (response.type === "opaque") {
const err = new Error("Opaque response")
err["status"] = response.status
err["response"] = response
throw err
}
return response[conf.errorType || "text"]().then(msg => {
// Enhances the error object
const err = new Error(msg)
Expand All @@ -85,11 +91,11 @@ export const resolver = (wretcher: Wretcher) => {
return promise.catch(err => {
timeout.clear()
const error = err instanceof WretchErrorWrapper ? err.error : err
if(err instanceof WretchErrorWrapper && catchers.has("__fromFetch"))
if (err instanceof WretchErrorWrapper && catchers.has("__fromFetch"))
return catchers.get("__fromFetch")(error, wretcher)
else if(catchers.has(error.status))
else if (catchers.has(error.status))
return catchers.get(error.status)(error, wretcher)
else if(catchers.has(error.name))
else if (catchers.has(error.name))
return catchers.get(error.name)(error, wretcher)
else
throw error
Expand Down Expand Up @@ -151,7 +157,7 @@ export const resolver = (wretcher: Wretcher) => {
/**
* Returns the automatically generated AbortController alongside the current wretch response as a pair.
*/
controller: () => [ fetchController, responseChain ],
controller: () => [fetchController, responseChain],
/**
* Catches an http response with a specific error code or name and performs a callback.
*/
Expand Down

0 comments on commit bbb5912

Please sign in to comment.