Skip to content

Commit cfd45a9

Browse files
committed
🎨 Enhance errorType and allow disabling it
1 parent b4b4645 commit cfd45a9

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mix } from "./utils.js"
2-
import type { Config } from "./types.js"
2+
import type { Config, ErrorType } from "./types.js"
33

44
declare const global
55

@@ -94,7 +94,7 @@ export function setPolyfills(polyfills: object, replace = false) {
9494
*
9595
* If null, defaults to "text".
9696
*/
97-
export function setErrorType(errorType: string) {
97+
export function setErrorType(errorType: ErrorType) {
9898
config.errorType = errorType
9999
}
100100

src/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { mix, extractContentType, isLikelyJsonMime } from "./utils.js"
22
import { JSON_MIME, CONTENT_TYPE_HEADER, CATCHER_FALLBACK } from "./constants.js"
33
import { resolver } from "./resolver.js"
44
import config from "./config.js"
5-
import type { Wretch } from "./types.js"
5+
import type { Wretch, ErrorType } from "./types.js"
66

77
export const core: Wretch = {
88
_url: "",
@@ -16,7 +16,7 @@ export const core: Wretch = {
1616
addon(addon) {
1717
return { ...this, _addons: [...this._addons, addon], ...addon.wretch }
1818
},
19-
errorType(errorType: string) {
19+
errorType(errorType: ErrorType) {
2020
return {
2121
...this,
2222
_config: {

src/resolver.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,29 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {
5656
err["cause"] = referenceError
5757
err.stack = err.stack + "\nCAUSE: " + referenceError.stack
5858
err.response = response
59+
err.status = response.status
5960
err.url = finalUrl
61+
6062
if (response.type === "opaque") {
6163
throw err
6264
}
63-
return response.text().then((body: string) => {
64-
err.message = body
65-
if (config.errorType === "json" || response.headers.get("Content-Type")?.split(";")[0] === "application/json") {
66-
try { err.json = JSON.parse(body) } catch (e) { /* ignore */ }
65+
66+
const jsonErrorType = config.errorType === "json" || response.headers.get("Content-Type")?.split(";")[0] === "application/json"
67+
const bodyPromise =
68+
!config.errorType ? Promise.resolve(response.body) :
69+
jsonErrorType ? response.text() :
70+
response[config.errorType]()
71+
72+
return bodyPromise.then((body: unknown) => {
73+
err.message = typeof body === "string" ? body : response.statusText
74+
if(body) {
75+
if(jsonErrorType && typeof body === "string") {
76+
err.text = body
77+
err.json = JSON.parse(body)
78+
} else {
79+
err[config.errorType] = body
80+
}
6781
}
68-
err.text = body
69-
err["status"] = response.status
7082
throw err
7183
})
7284
}

src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface Wretch<Self = unknown, Chain = unknown, Resolver = undefined> {
8383
* @category Helpers
8484
* @param method - The method to call on the Fetch response to read the body and use it as the Error message
8585
*/
86-
errorType(this: Self & Wretch<Self, Chain, Resolver>, method: string): this
86+
errorType(this: Self & Wretch<Self, Chain, Resolver>, method: ErrorType): this
8787

8888
/**
8989
* Sets non-global polyfills - for instance in browserless environments.
@@ -733,12 +733,14 @@ export interface WretchResponseChain<T, Self = unknown, R = undefined> {
733733
fetchError: (this: Self & WretchResponseChain<T, Self, R>, cb: WretchErrorCallback<T, Self, R>) => this,
734734
}
735735

736+
export type ErrorType = "text" | "json" | "blob" | "formData" | "arrayBuffer" | undefined | null
737+
736738
/**
737739
* Configuration object.
738740
*/
739741
export type Config = {
740742
options: object;
741-
errorType: string;
743+
errorType: ErrorType;
742744
polyfills: object;
743745
polyfill(p: "fetch", doThrow?: boolean): typeof fetch;
744746
polyfill(p: "FormData", doThrow: boolean, instance: true, ...args: ConstructorParameters<typeof FormData>): FormData;

test/node/wretch.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,16 @@ describe("Wretch", function () {
575575
})
576576
.res(_ => fail("I should never be called because an error was thrown"))
577577
.then(_ => expect(_).toBe(undefined))
578+
// Disabled
579+
await wretch(`${_URL}/json500raw`)
580+
.errorType(null)
581+
.get()
582+
.internalError(error => {
583+
expect(error.json).toEqual(undefined)
584+
expect(error.text).toEqual(undefined)
585+
})
586+
.res(_ => fail("I should never be called because an error was thrown"))
587+
.then(_ => expect(_).toBe(undefined))
578588
// Global
579589
wretch.errorType("json")
580590
await wretch(`${_URL}/json500raw`)

0 commit comments

Comments
 (0)