|
1 | 1 | import { middlewareHelper } from "./middleware.js" |
2 | 2 | import { mix } from "./utils.js" |
3 | 3 | import type { Wretch, WretchResponse, WretchResponseChain, WretchError as WretchErrorType } from "./types.js" |
4 | | -import { FETCH_ERROR } from "./constants.js" |
| 4 | +import { FETCH_ERROR, CATCHER_FALLBACK } from "./constants.js" |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * This class inheriting from Error is thrown when the fetch response is not "ok". |
@@ -47,7 +47,7 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => { |
47 | 47 | const referenceError = new Error() |
48 | 48 | const throwingPromise: Promise<void | WretchResponse> = _fetchReq |
49 | 49 | .catch(error => { |
50 | | - throw { __wrap: error } |
| 50 | + throw { [FETCH_ERROR]: error } |
51 | 51 | }) |
52 | 52 | .then(response => { |
53 | 53 | if (!response.ok) { |
@@ -75,17 +75,22 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => { |
75 | 75 | // Wraps the Promise in order to dispatch the error to a matching catcher |
76 | 76 | const catchersWrapper = <T>(promise: Promise<T>): Promise<void | T> => { |
77 | 77 | return promise.catch(err => { |
78 | | - const error = err.__wrap || err |
| 78 | + const fetchErrorFlag = err.hasOwnProperty(FETCH_ERROR) |
| 79 | + const error = fetchErrorFlag ? err[FETCH_ERROR] : err |
79 | 80 |
|
80 | 81 | const catcher = |
81 | | - (error.status && catchers.get(error.status)) || |
82 | | - catchers.get(error.name) || ( |
83 | | - err.__wrap && catchers.has(FETCH_ERROR) && catchers.get(FETCH_ERROR) |
| 82 | + (error?.status && catchers.get(error.status)) || |
| 83 | + catchers.get(error?.name) || ( |
| 84 | + fetchErrorFlag && catchers.has(FETCH_ERROR) && catchers.get(FETCH_ERROR) |
84 | 85 | ) |
85 | 86 |
|
86 | 87 | if (catcher) |
87 | 88 | return catcher(error, wretch) |
88 | 89 |
|
| 90 | + const catcherFallback = catchers.get(CATCHER_FALLBACK) |
| 91 | + if (catcherFallback) |
| 92 | + return catcherFallback(error, wretch) |
| 93 | + |
89 | 94 | throw error |
90 | 95 | }) |
91 | 96 | } |
|
0 commit comments