Skip to content

Commit

Permalink
🎨 Harmonize WretchError types
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Aug 18, 2022
1 parent 30e6b98 commit 1981837
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ factory.options = setOptions
factory.errorType = setErrorType
/** {@inheritDoc setPolyfills} */
factory.polyfills = setPolyfills
/** {@inheritDoc WretchError } */
factory.WretchError = WretchError

export default factory
12 changes: 9 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { middlewareHelper } from "./middleware.js"
import { mix } from "./utils.js"
import type { Wretch, WretchResponse, WretchResponseChain } from "./types.js"
import type { Wretch, WretchResponse, WretchResponseChain, WretchError as WretchErrorType } from "./types.js"
import { FETCH_ERROR } from "./constants.js"

/**
* This class inheriting from Error is thrown when the fetch response is not "ok".
* It extends Error and adds status, text and body fields.
*/
export class WretchError extends Error { }
export class WretchError extends Error implements WretchErrorType {
status: number
response: WretchResponse
text?: string
json?: any
}

export const resolver = <T, Chain, R>(wretch: Wretch<T, Chain, R>) => {
const {
Expand Down Expand Up @@ -36,7 +42,7 @@ export const resolver = <T, Chain, R>(wretch: Wretch<T, Chain, R>) => {
// Enhance the error object
err["cause"] = referenceError
err.stack = err.stack + "\nCAUSE: " + referenceError.stack
err["response"] = response
err.response = response
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 type WretchError = Error & { status: number, response: WretchResponse, text?: string, json?: any }
export interface WretchError extends Error { status: number, response: WretchResponse, text?: string, json?: any }
/**
* Callback provided to catchers on error. Contains the original wretch instance used to perform the request.
*/
Expand Down

0 comments on commit 1981837

Please sign in to comment.