@hey-api/client-axios resolves with { status: 200, error, data: {} } instead of rejecting on HTTP 400/401 (even with throwOnError: true) #2697
-
My server returns 400 Bad Request (and 401 Unauthorized for a follow-up call), but the SDK generated with @hey-api/client-axios does not throw. Instead, the promise resolves to an object like:
This happens both with throwOnError: true and without it. As a result, try/catch never triggers. Expected behavior Actual behavior OpenAPI generator config:
Runtime config passed via runtimeConfigPath:
Is the { status: 200, error, data } wrapper the intended runtime shape even when throwOnError: true? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @maksymhryzodub! I'm Dosu and I’m helping the openapi-ts team. The documentation does not confirm that throwOnError: true causes @hey-api/client-axios to reject promises on non-2xx responses, nor does it document the { status: 200, error, data } wrapper as the intended runtime shape for errors. In fact, the migration guide notes that not every client supports throwOnError, so it may have no effect in some cases, including this one see migration guide. The client is a thin wrapper over Axios and exposes the underlying Axios instance, so you can add a response interceptor to customize error handling. To make the client reject on 4xx/5xx responses (so try/catch works as expected), you can add an interceptor like this: import { client } from 'client/client.gen';
client.instance.interceptors.response.use(
response => response,
error => Promise.reject(error)
); This restores standard Axios error handling, so non-2xx responses will reject and be caught by try/catch see docs. If you need more control, you can also provide your own Axios instance via setConfig. Let me know if you want an example tailored to your setup! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Hi @maksymhryzodub! I'm Dosu and I’m helping the openapi-ts team.
The documentation does not confirm that throwOnError: true causes @hey-api/client-axios to reject promises on non-2xx responses, nor does it document the { status: 200, error, data } wrapper as the intended runtime shape for errors. In fact, the migration guide notes that not every client supports throwOnError, so it may have no effect in some cases, including this one see migration guide.
The client is a thin wrapper over Axios and exposes the underlying Axios instance, so you can add a response interceptor to customize error handling. To make the client reject on 4xx/5xx responses (so try/catch works as expected), you ca…