Skip to content

Commit 0486e49

Browse files
committed
🐛 Fix custom error conflicting with catchers
1 parent 68d1a0f commit 0486e49

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

.github/workflows/node.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Build
2929
run: npm run build
3030
- name: Unit tests
31-
run: npm run test
31+
run: npm run test:node
3232
- name: Coveralls
3333
if: ${{ success() }}
3434
uses: coverallsapp/github-action@master

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@
116116
"prebuild": "rimraf dist && rimraf coverage && npm run lint",
117117
"build": "tsc && tsc --project tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json && rolldown -c ./rolldown.config.ts",
118118
"mock": "node scripts/mockServer.js",
119-
"test": "node scripts/runWithMockServer.js \"c8 node --import tsx --test test/node/*.spec.ts --test test/node/**/*.spec.ts\"",
119+
"test": "npm run test:node && npm run test:browser && npm run test:deno && npm run test:bun && npm run test:snippets",
120+
"test:node": "node scripts/runWithMockServer.js \"c8 node --import tsx --test test/node/*.spec.ts --test test/node/**/*.spec.ts\"",
120121
"test:browser": "node scripts/runWithMockServer.js web-test-runner",
121122
"test:browser:watch": "node scripts/runWithMockServer.js \"web-test-runner --watch\"",
122123
"test:deno": "node scripts/runWithMockServer.js \"deno test --allow-net --allow-read --no-check --sloppy-imports --config test/deno/deno.json test/deno/wretch.spec.ts\"",

src/resolver.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { middlewareHelper } from "./middleware.js"
22
import type { Wretch, WretchResponse, WretchResponseChain, WretchError as WretchErrorType } from "./types.js"
3-
import { FETCH_ERROR, CATCHER_FALLBACK } from "./constants.js"
3+
import { CATCHER_FALLBACK, FETCH_ERROR } from "./constants.js"
44

55
/**
66
* This class inheriting from Error is thrown when the fetch response is not "ok".
@@ -45,9 +45,6 @@ export const resolver = <T, Chain, R, E>(wretch: T & Wretch<T, Chain, R, E>) =>
4545
// Throws on an http error
4646
const referenceError = new Error()
4747
const throwingPromise: Promise<void | WretchResponse> = _fetchReq
48-
.catch(error => {
49-
throw { [FETCH_ERROR]: error }
50-
})
5148
.then(async response => {
5249
if (!response.ok) {
5350
const err = new WretchError()
@@ -57,19 +54,14 @@ export const resolver = <T, Chain, R, E>(wretch: T & Wretch<T, Chain, R, E>) =>
5754
err.status = response.status
5855
err.url = finalUrl
5956

60-
if (response.type === "opaque") {
61-
err.message = response.statusText
62-
throw err
63-
}
64-
65-
if (errorTransformer) {
66-
throw await errorTransformer(err, response)
67-
}
68-
69-
try {
70-
err.message = await response.text()
71-
} catch {
57+
if (response.type === "opaque" || errorTransformer) {
7258
err.message = response.statusText
59+
} else {
60+
try {
61+
err.message = await response.text()
62+
} catch {
63+
err.message = response.statusText
64+
}
7365
}
7466

7567
throw err
@@ -78,16 +70,18 @@ export const resolver = <T, Chain, R, E>(wretch: T & Wretch<T, Chain, R, E>) =>
7870
})
7971
// Wraps the Promise in order to dispatch the error to a matching catcher
8072
const catchersWrapper = <T>(promise: Promise<T>): Promise<void | T> =>
81-
promise.catch(err => {
82-
const fetchErrorFlag = FETCH_ERROR in err
83-
const error = fetchErrorFlag ? err[FETCH_ERROR] : err
73+
promise.catch(async error => {
8474

8575
const catcher =
86-
(error?.status && catchers.get(error.status)) ||
76+
catchers.get(error?.status) ||
8777
catchers.get(error?.name) ||
88-
(fetchErrorFlag && catchers.get(FETCH_ERROR)) ||
78+
(!(error instanceof WretchError) && catchers.get(FETCH_ERROR)) ||
8979
catchers.get(CATCHER_FALLBACK)
9080

81+
if(error.response && errorTransformer) {
82+
error = await errorTransformer(error, error.response)
83+
}
84+
9185
if (catcher)
9286
return catcher(error, wretch)
9387

test/shared/wretch.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,13 @@ export function createWretchTests(ctx: TestContext): void {
529529

530530
it("should allow transforming errors with fully typed error bodies", async function () {
531531
await wretch(`${_URL}/json500raw`)
532-
.customError(async (error, response) => {
533-
return { ...error, message: response.statusText, json: await response.json() }
532+
.customError(async (_error, response) => {
533+
const err = new Error(response.statusText)
534+
err["json"] = await response.json()
535+
return err
534536
})
535537
.get()
536-
.internalError(error => {
538+
.internalError((error: any) => {
537539
expect(error.json).toEqual({ error: 500, message: "ok" })
538540
expect(error.message).toEqual("Internal Server Error")
539541
})

0 commit comments

Comments
 (0)