Skip to content

Commit

Permalink
🏭 Add error cause
Browse files Browse the repository at this point in the history
Should solve #98
  • Loading branch information
elbywan committed Jul 3, 2022
1 parent 77e85dc commit 9a0a387
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const resolver = <T, Chain>(wretch: Wretch<T, Chain>) => {
// The generated fetch request
const fetchRequest = middlewareHelper(middlewares)(config.polyfill("fetch"))(url, finalOptions)
// Throws on an http error
const referenceError = new Error()
const throwingPromise: Promise<void | WretchResponse> = fetchRequest
.catch(error => {
throw new WretchErrorWrapper(error)
Expand All @@ -95,12 +96,16 @@ export const resolver = <T, Chain>(wretch: Wretch<T, Chain>) => {
if (!response.ok) {
if (response.type === "opaque") {
const err = new Error("Opaque response")
err["cause"] = referenceError
err.stack = err.stack + "\nCAUSE: " + referenceError.stack
err["response"] = response
throw err
}
return response[config.errorType || "text"]().then(body => {
// Enhances the error object
const err = new Error(body)
err["cause"] = referenceError
err.stack = err.stack + "\nCAUSE: " + referenceError.stack
err[config.errorType || "text"] = body
err["response"] = response
err["status"] = response.status
Expand Down

0 comments on commit 9a0a387

Please sign in to comment.