diff --git a/src/index.ts b/src/index.ts index 5d8e463..7ab175d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,6 @@ factory.options = setOptions factory.errorType = setErrorType /** {@inheritDoc setPolyfills} */ factory.polyfills = setPolyfills -/** {@inheritDoc WretchError } */ factory.WretchError = WretchError export default factory diff --git a/src/resolver.ts b/src/resolver.ts index 66603e5..6167849 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -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 = (wretch: Wretch) => { const { @@ -36,7 +42,7 @@ export const resolver = (wretch: Wretch) => { // 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 } diff --git a/src/types.ts b/src/types.ts index 4c77431..e405d7f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -724,7 +724,7 @@ export type WretchOptions = Record /** * 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. */