Skip to content

Commit 4347e4b

Browse files
committed
🏭 Add request parameter to .customError transformer
The customError transformer now receives the wretch request instance as a third parameter, allowing error handlers to access the original request context when transforming errors.
1 parent 745b8a0 commit 4347e4b

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

MIGRATION_V2_V3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ interface ApiError {
121121
}
122122

123123
const api = wretch("http://server")
124-
.customError<ApiError>(async (error, response) => {
124+
.customError<ApiError>(async (error, response, request) => {
125125
return { ...error, json: await response.json() }
126126
})
127127

RECIPES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ interface ApiError {
5555
}
5656

5757
const api = wretch('https://jsonplaceholder.typicode.com')
58-
.customError(async (error, response) => {
58+
.customError(async (error, response, request) => {
5959
const body = await response.json().catch(() => ({ message: 'Unknown error' }));
6060
return { ...error, apiError: body as ApiError };
6161
});
@@ -78,7 +78,7 @@ try {
7878
import wretch from 'wretch';
7979

8080
const api = wretch('https://jsonplaceholder.typicode.com')
81-
.customError(async (error, response) => {
81+
.customError(async (error, response, request) => {
8282
const contentType = response.headers.get('content-type') || '';
8383
let errorBody;
8484

src/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const resolver = <T, Chain, R, E>(wretch: T & Wretch<T, Chain, R, E>) =>
7979
catchers.get(CATCHER_FALLBACK)
8080

8181
if(error.response && errorTransformer) {
82-
error = await errorTransformer(error, error.response)
82+
error = await errorTransformer(error, error.response, wretch)
8383
}
8484

8585
if (catcher)

src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface Wretch<Self = unknown, Chain = unknown, Resolver = undefined, E
2626
/**
2727
* @private @internal
2828
*/
29-
_errorTransformer?: <T>(error: WretchError, response: WretchResponse) => Promise<T> | T,
29+
_errorTransformer?: <T>(error: WretchError, response: WretchResponse, request: Wretch<any, any, any, any>) => Promise<T> | T,
3030
/**
3131
* @private @internal
3232
*/
@@ -311,7 +311,7 @@ export interface Wretch<Self = unknown, Chain = unknown, Resolver = undefined, E
311311
* }
312312
*
313313
* const api = wretch("https://api.example.com")
314-
* .customError<ApiError>(async (error, response) => {
314+
* .customError<ApiError>(async (error, response, request) => {
315315
* const json = await response.json();
316316
* return { ...error, ...json };
317317
* });
@@ -326,9 +326,9 @@ export interface Wretch<Self = unknown, Chain = unknown, Resolver = undefined, E
326326
* ```
327327
*
328328
* @category Helpers
329-
* @param transformer - A function that receives the error and response, and returns the transformed error with custom properties
329+
* @param transformer - A function that receives the error, response, and request, and returns the transformed error with custom properties
330330
*/
331-
customError<T extends (ErrorType extends undefined ? any : ErrorType)>(this: Self & Wretch<Self, Chain, Resolver, ErrorType>, transformer: (error: WretchError, response: WretchResponse) => Promise<T> | T): Self & Wretch<Self, Chain, Resolver, T>
331+
customError<T extends (ErrorType extends undefined ? any : ErrorType)>(this: Self & Wretch<Self, Chain, Resolver, ErrorType>, transformer: (error: WretchError, response: WretchResponse, request: Self & Wretch<Self, Chain, Resolver, ErrorType>) => Promise<T> | T): Self & Wretch<Self, Chain, Resolver, T>
332332

333333
/**
334334
* Defer one or multiple request chain methods that will get called just before the request is sent.

0 commit comments

Comments
 (0)