I am currently trying to migrate a project that uses axios 0.26.0 to uses Axios 1.6.0. I encountered an issue with the AxiosError interface. In version 0.26.0, the AxiosError interface was as follows:
export interface AxiosError<T = any, D = any> extends Error {
config: AxiosRequestConfig<D>;
code?: string;
request?: any;
response?: AxiosResponse<T, D>;
isAxiosError: boolean;
toJSON: () => object;
}
However, in version 1.6.0, this interface no longer exists. Instead, I only see:
type AxiosError<T = unknown, D = any> = InternalAxiosError<T, D>;
I have a piece of code in the old project like this:
export const throwHttpError = (error: AxiosError): Promise<IHttpErrorResponse> => {
console.log(error?.message);
const isCancelError = axios.isCancel(error);
if (isCancelError) {
return Promise.reject<IHttpErrorResponse>(
httpError({
statusCode: 200,
errorMessage: error.message,
errorKey: HttpErrorEvent.CALL_CANCELED,
})
);
}
if (error?.message === NETWORK_ERROR_MESSAGE) {
return Promise.reject<IHttpErrorResponse>(
httpError({
errorMessage: 'Sorry, an error has occurred. Please refresh the page.',
errorKey: HttpErrorEvent.NETWORK_ERROR,
})
);
}
//...
}
The above code tries to access error?.message, but this property does not exist in the new version. When I compile this file using tsc, I get the following exception: Property ‘message’ does not exist on type ‘never’.
Does anyone have any suggestions on how I can migrate this old code to be compatible with the new AxiosError type in Axios 1.6.0? Thanks in advance for any help.
Where are you importing
AxiosError
from? The type definition for it in 1.6.0 is not what you’re seeing ~ github.com/axios/axios/blob/v1.6.0/index.d.ts#L398