Axios validateStatus: () => true prevents axios interceptors from executing
See original GitHub issueRE: #2726 and #2806, setting validateStatus: () => true
makes it impossible to intercept response errors at the axios level (e.g. for automatically retrying 401 responses with updated auth tokens, logging, etc.)
Perhaps catching exceptions thrown by the axios request/response and forwarding the response would offer greater flexibility. This way axios interceptors can still execute and the client can still handle failed responses.
I don’t know if this covers all cases, but here’s a possible implementation:
Original code:
getItemById(id: number): Promise<Item> {
let url_ = this.baseUrl + "/items/{id}";
if (id === undefined || id === null)
throw new Error("The parameter 'id' must be defined.");
url_ = url_.replace("{id}", encodeURIComponent("" + id));
url_ = url_.replace(/[?&]$/, "");
let options_ = <AxiosRequestConfig>{
validateStatus: () => true,
method: "GET",
url: url_,
headers: {
"Accept": "application/json"
}
};
return this.instance.request(options_).then((_response: AxiosResponse) => {
return this.processGetItemById(_response);
});
}
Updated code:
getItemById(id: number): Promise<Item> {
let url_ = this.baseUrl + "/items/{id}";
if (id === undefined || id === null)
throw new Error("The parameter 'id' must be defined.");
url_ = url_.replace("{id}", encodeURIComponent("" + id));
url_ = url_.replace(/[?&]$/, "");
let options_ = <AxiosRequestConfig>{
// no more validateStatus: () => true,
method: "GET",
url: url_,
headers: {
"Accept": "application/json"
}
};
return this.instance.request(options_).catch((_error: any) => {
// if it's an axios error with response, forward the response for processing,
// otherwise just throw because the process method won't be able to handle it anyway
if (isAxiosError(_error) && _error.response) {
return _error.response
} else {
throw _error
}
}).then((_response: AxiosResponse) => {
return this.processGetItemById(_response);
});
}
where isAxiosError
is a simple type guard:
function isAxiosError(obj: any): obj is AxiosError {
return obj.isAxiosError === true;
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to ignore interceptors in axios as a parameter?
I have axios interceptors, is it possible to ignore interceptors for one request by invoke the axios method? something like: axios.post('/path/ ...
Read more >Setting up Axios Interceptors for all HTTP calls in an application
Interceptors are a feature that allows an application to intercept requests or responses before they are handled by the .then() or the .catch()...
Read more >How to use Axios interceptors to handle API error responses
The axios.intercepotrs.request.use(config) function has one argument, which is the configuration of the headers, while the axios.intercepotrs.
Read more >[Axios] Interceptor - Error handling
This article shows how to integrate the custom error to handle non-2XX response and timeout request, so that we can show customized message ......
Read more >Axios redirect follow. We are trying to implement short URLs w
Handling Redirects with Axios: By default, Axios does not automatically follow redirects. Start using follow-redirects in your project by running `npm i follow- ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@GordonBlahut I did think of that. The issue I had is that I have an interceptor that I want to run on every api call to check for 401 and will attempt to refresh the access token. I didn’t want to have to pass in a new instance of axios into each instantiation of each client when they all needed the same interceptor.
@bsell93 You can pass an axios instance into the client constructor as well. That way you can wire-up any interceptors on the axios instance outside of the client and avoid messing around with the generated code in case you need to re-generate it periodically.