I have a file called agent.ts that contains axios interceptors. In those interceptors I watch for any status code that is >= 400 and <= 500. When such error occurs, I am invoking react-toastify toast.
Everything works perfect so far. What I want to be able to do though, is to use html as body of the message. Usually I would do that by creating a custom component and then using the component as body of the toast message. But because I am using react-toastify outsode of component in a regular typescript file, I am getting an compilation error.
Here is part of my code:
axios.interceptors.response.use(undefined, async (error) => {
if (error.message === 'Network Error' && !error.response) {
toast.error('Network error - make sure API is running!');
}
const { status, data } = error.response;
if (status >= 400 && status < 500) {
if (!data || !data.errors) {
toast.error(`${status} API error. Please check the terminal!`);
return;
}
console.warn('API Errors:', data.errors);
Object.keys(data.errors).forEach(function (key) {
toast.warn(data.errors[key].toString());
});
}
if (status === 500) {
toast.error('Server error - check the terminal for more info!');
}
throw error.response;
});
I am attaching a screenshot of the compile error that I see:
Can you please advice me what is the best approach here ?