I would like to have a general way to show the errors. I tried to add a response interceptor, but my state does not get updated.
interceptor.js
import {showError} from "./actions";
const axiosInstance = axios.create();
axiosInstance.interceptors.response.use(
response => response,
error => {
showError(error);
}
);
export default axiosInstance;
actions.js
export const showError = (error) => {
return ({
type: SHOW_ERROR,
payload: error.response
})
}
reducer.js
const INITIAL_STATE = {errorData: ''};
const reducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case SHOW_ERROR:
return {
...state,
errorData: action.payload.data.translationKey,
}
}
}
I have all the imports in place, I can see the error in the actions, but my state does not get updated. Reducer is not called.
Is there a way to update the state directly from the interceptor? I want to avoid catching errors for each api call.