[v3.x] How to globally catch errors from useHttp() #3009
-
|
Hello, I would like to know how to globally catch errors when doing HTTP requests using useHttp(). Unfortunately, using In addition, it would be awesome to add same callbacks options on useHttp() & useForm() like we have on router.visit() : onHttpException and onNetworkError. Thanks in advance Ps : perhaps add some documentations about each Event Callbacks on router.visit() would be well because I wasn't understanding that onError doesn't catch all errors (like the most of other librairies) but only validation errors 422 (if I understood well). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
You can intercept requests or responses before they are handled by by using axios. Has shown in the docs.
import Axios, { type AxiosError } from 'axios';
const { VITE_APP_DEBUG, VITE_APP_URL } = import.meta.env
const axios = Axios.create({
baseURL: VITE_APP_URL,
headers: {
Accept: 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
withCredentials: true,
withXSRFToken: true
});
axios.interceptors.request.use(
configuration => {
if (VITE_APP_DEBUG) console.log('InternalAxiosRequestConfig', configuration);
return configuration;
},
(error: AxiosError) => {
if (VITE_APP_DEBUG) console.error('AxiosError', error);
return Promise.reject(error);
}
);
axios.interceptors.response.use(
response => {
if (VITE_APP_DEBUG) console.log('AxiosResponse', response);
return response;
},
(error: AxiosError) => {
if(VITE_APP_DEBUG) console.error('AxiosError', error);
return Promise.reject(error);
}
);
export { axios };
import { axios } from '@/lib/axios'
import { axiosAdapter } from '@inertiajs/core'
createInertiaApp({
http: axiosAdapter(axios),
// ...
}) |
Beta Was this translation helpful? Give feedback.
It's already here. See my latest comment edit.