I am using Axios to make a request to a server. When the server responds with a code of anything other than a 200, I get an error that the request failed and the status code is undefined
.
I would like to know the status code so I can decide how to respond to it.
Example response:
{
message: 'Request failed with status code 500',
stack: 'AxiosError: Request failed with status code 500n …/./node_modules/axios/lib/adapters/xhr.js:125:66)',
code: 'ERR_BAD_RESPONSE',
status: undefined,
method: 'get',
…
}
code: "ERR_BAD_RESPONSE"
message: "Request failed with status code 500"
method : "get"
stack: "AxiosError: Request failed with status code 500n at settle (webpack-internal:///./node_modules/axios/lib/core/settle.js:24:12)n at XMLHttpRequest.onloadend (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:125:66)"
status: undefined
Example request:
return axios
.get(url, {
params: flattenedParams
})
.then((response: AxiosResponse) => {
if (response.status == 500) {
throw new Error("server side error");
}
return response;
});