@@ -75,9 +75,9 @@ For the above spec, the TanStack Query client generates:
75
75
` ` ` typescript
76
76
// Error type is automatically inferred as :
77
77
type CreateUserError =
78
- | { status : 400; data: ValidationError }
79
- | { status : 409; data: ConflictError }
80
- | { status : 500; data: ServerError }
78
+ | Omit<Response, 'status'> & { status : 400; data: ValidationError }
79
+ | Omit<Response, 'status'> & { status : 409; data: ConflictError }
80
+ | Omit<Response, 'status'> & { status : 500; data: ServerError }
81
81
` ` `
82
82
83
83
## Usage Examples
@@ -93,6 +93,9 @@ function CreateUserForm() {
93
93
...tanstackApi.mutation("post", "/users").mutationOptions,
94
94
onError : (error) => {
95
95
// error is fully typed based on your OpenAPI spec!
96
+ // error is also a Response instance with additional data property
97
+ console.log(error instanceof Response); // true
98
+
96
99
if (error.status === 400) {
97
100
// error.data is typed as ValidationError
98
101
console.error('Validation failed:', error.data.message);
@@ -138,6 +141,10 @@ function AdvancedCreateUserForm() {
138
141
}).mutationOptions ,
139
142
onError : (error ) => {
140
143
// Same typed error handling as above
144
+ // error is also a Response instance
145
+ console .log (error .ok ); // false
146
+ console .log (error .headers ); // Response headers
147
+
141
148
switch (error .status ) {
142
149
case 400 :
143
150
toast .error (` Validation: ${error .data .fields .join (' , ' )} ` );
@@ -206,13 +213,40 @@ const handleError = (error: CreateUserError) => {
206
213
207
214
## Error Structure
208
215
209
- All errors thrown by TanStack Query mutations follow this structure :
216
+ All errors thrown by TanStack Query mutations are ** Response-like objects ** that extend the native Response with additional type safety :
210
217
211
218
``` typescript
212
- interface ApiError <TData = unknown > {
213
- status: number ; // HTTP status code (400-511)
219
+ interface ApiError <TData = unknown > extends Omit < Response , ' status ' > {
220
+ status: number ; // HTTP status code (400-511) - properly typed
214
221
data: TData ; // Typed error response body
222
+ // All other Response properties are available:
223
+ // ok: boolean
224
+ // headers: Headers
225
+ // url: string
226
+ // etc.
215
227
}
216
228
```
217
229
218
- This makes error handling predictable and type-safe across your entire application.
230
+ ### Key Benefits of Response-like Errors
231
+
232
+ - ** instanceof Response** : Error objects are proper Response instances
233
+ - ** Consistent API** : Both success and error responses follow the same Response pattern
234
+ - ** Full Response Access** : Access to headers, URL, and other Response properties
235
+ - ** Type Safety** : The ` status ` property is properly typed based on configured error status codes
236
+
237
+ ### Example Usage
238
+
239
+ ``` typescript
240
+ try {
241
+ await mutation .mutationFn (params );
242
+ } catch (error ) {
243
+ console .log (error instanceof Response ); // true
244
+ console .log (error .ok ); // false
245
+ console .log (error .status ); // 400, 401, etc. (properly typed)
246
+ console .log (error .data ); // Typed error response body
247
+ console .log (error .headers .get (' content-type' )); // Response headers
248
+ console .log (error .url ); // Request URL
249
+ }
250
+ ```
251
+
252
+ This makes error handling predictable and type-safe across your entire application while maintaining consistency with the Response API.
0 commit comments