Skip to content

Commit

Permalink
🏭 Add url property to WretchError
Browse files Browse the repository at this point in the history
Solves #157
  • Loading branch information
elbywan committed Dec 12, 2022
1 parent d18ef68 commit a1f6ac6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FETCH_ERROR } from "./constants.js"
export class WretchError extends Error implements WretchErrorType {
status: number
response: WretchResponse
url: string
text?: string
json?: any
}
Expand All @@ -29,7 +30,11 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {
const finalOptions = mix(config.options, opts)
addons.forEach(addon => addon.beforeRequest && addon.beforeRequest(wretch, finalOptions))
// The generated fetch request
const _fetchReq = middlewareHelper(middlewares)(config.polyfill("fetch"))(url, finalOptions)
let finalUrl = url
const _fetchReq = middlewareHelper(middlewares)((url, options) => {
finalUrl = url
return config.polyfill("fetch")(url, options)
})(url, finalOptions)
// Throws on an http error
const referenceError = new Error()
const throwingPromise: Promise<void | WretchResponse> = _fetchReq
Expand All @@ -43,6 +48,7 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {
err["cause"] = referenceError
err.stack = err.stack + "\nCAUSE: " + referenceError.stack
err.response = response
err.url = finalUrl
if (response.type === "opaque") {
throw err
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ export type WretchOptions = Record<string, any>
/**
* An Error enhanced with status, text and body.
*/
export interface WretchError extends Error { status: number, response: WretchResponse, text?: string, json?: any }
export interface WretchError extends Error { status: number, response: WretchResponse, url: string, text?: string, json?: any }
/**
* Callback provided to catchers on error. Contains the original wretch instance used to perform the request.
*/
Expand Down
1 change: 1 addition & 0 deletions test/node/wretch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ describe("Wretch", function () {
.res(_ => fail("Authenticated route should not respond without credentials."))
} catch (e) {
expect(e.status).toBe(401)
expect(e.url).toBe(_URL + "/basicauth")
}

const res = await wretch(_URL + "/basicauth")
Expand Down

0 comments on commit a1f6ac6

Please sign in to comment.