Skip to content

Commit 9a8faad

Browse files
committed
🐛 Fix .fetchError catching user errors
Should fix #318
1 parent a7c2185 commit 9a8faad

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/resolver.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const resolver = <T, Chain, R, E, CatcherResult>(wretch: T & Wretch<T, Ch
2424
const {
2525
_url: url,
2626
_options: opts,
27-
_fetch: customFetch,
27+
_fetch,
2828
_errorTransformer: errorTransformer,
2929
_catchers: _catchers,
3030
_resolvers: resolvers,
@@ -39,12 +39,17 @@ export const resolver = <T, Chain, R, E, CatcherResult>(wretch: T & Wretch<T, Ch
3939
let finalUrl = url
4040
const _fetchReq = middlewareHelper(middlewares)((url, options) => {
4141
finalUrl = url
42-
const fetchImpl = customFetch || fetch
42+
const fetchImpl = _fetch || fetch
4343
return fetchImpl(url, options)
4444
})(url, finalOptions)
4545
// Throws on an http error
4646
const referenceError = new Error()
4747
const throwingPromise: Promise<void | WretchResponse> = _fetchReq
48+
.catch(error => {
49+
// Interect fetch errors and mark them with a special symbol so that they can be caught by a matching catcher
50+
error[FETCH_ERROR] = true
51+
throw error
52+
})
4853
.then(async response => {
4954
if (!response.ok) {
5055
const err = new WretchError()
@@ -75,7 +80,7 @@ export const resolver = <T, Chain, R, E, CatcherResult>(wretch: T & Wretch<T, Ch
7580
const catcher =
7681
catchers.get(error?.status) ||
7782
catchers.get(error?.name) ||
78-
(!(error instanceof WretchError) && catchers.get(FETCH_ERROR)) ||
83+
(error?.[FETCH_ERROR] && catchers.get(FETCH_ERROR)) ||
7984
catchers.get(CATCHER_FALLBACK)
8085

8186
if (error.response && errorTransformer) {

test/mock.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ export function launch(port) {
211211
return "ok"
212212
})
213213

214+
server.get("/network-error", async request => {
215+
request.socket.destroy()
216+
})
217+
214218
server.get("/*", async (request, reply) => {
215219
reply.status(404)
216220

test/shared/wretch.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,22 @@ export function createWretchTests(ctx: TestContext): void {
431431
expect(serverErrorCount).toBe(1)
432432
})
433433

434+
it("should catch fetch errors", async function () {
435+
let fetchErrorCount = 0
436+
437+
// Trigger a fetch error by calling an endpoint destroying the socket
438+
await wretch(_URL).url("/network-error").get().fetchError(() => { fetchErrorCount++ }).json()
439+
// .fetchError should have caught that
440+
expect(fetchErrorCount).toBe(1)
441+
442+
await wretch(_URL).url("/json").get()
443+
.fetchError(() => { fetchErrorCount-- })
444+
.json(() => { throw new Error() })
445+
.catch(() => { /* ignore */ })
446+
// .fetchError should not have been called, since the error was not a fetch error
447+
expect(fetchErrorCount).toBe(1)
448+
})
449+
434450
it("should support mixing single and array error IDs", async function () {
435451
let count = 0
436452

0 commit comments

Comments
 (0)