Search code examples
reactjsaxiosfetch

Not able to catch request error using axios


I have the following code, which is supposed to catch errors and read status code from there

 useEffect(async () => {
    try {
        const response = await apiSettingsSite.fetchProductsCalc();
        console.log(response)
    } catch (error) {
        console.log(error.response)
    }
},[])

but when 400 error happens i get null in the console from try block, code from catch is not even touched. Same with .then .catch


Solution

  • maybe you have a setting for axios i use like this:

    const client = axios.create({ baseURL, json: true })
    
    client.interceptors.response.use(undefined, async (err) => {
        return Promise.reject(err)
    })