Async waits with axioms not returning errors

I use async to wait with axios and am having trouble handling errors. Using regular promises (example 2 below), I can get an error object when killing my local server. However, using async wait errorcomes as undefined (example 1 below) Does anyone know why this would be

const instance = axios.create({
  baseURL: 'http://localhost:8000',
  timeout: 3000,
})

// example 1
try {
   await instance.get('/data/stores')
} catch (error) {
  console.log(error) // error is not defined
}
// example 2
return instance.get('/data/stores').catch(error => {
  console.log(error) // error is normal axios error
})
+4
source share
2 answers

It turned out that the error was inside catch, but my debugger did not recognize it.

+3
source

The response to the error is stored inside the property response. For some reason, you cannot see this in the Chrome console.

So in your catch block do:

console.log(error.response)
+2

Source: https://habr.com/ru/post/1685180/


All Articles