axios: Async await error message?

When using .catch() I get a proper error message with response, stack and message

but when i’m using async/await wrapped in a try/catch I only get config

Example:

 try {
  const { data } = await axios.get(forumUrl)
  resolve(data)
} catch (error) {
  console.log(error) //{config:{...}}
  reject(error)
}

Any idea why?

  • axios version: e.g.: latest

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 51
  • Comments: 17

Most upvoted comments

try returning error.response, it worked for me

 try {
  const { data } = await axios.get(forumUrl)
  resolve(data)
} catch (error) {
  console.log(error.response) // <---- 
  reject(error)
}

Hello, Are you using it in the browser or in node? If I do this

try {
  const { data } = await axios.get('http://foo.bar/')
} catch (error) {
  console.log(Object.keys(error), error.message); 
}

I get [“config”, “request”, “response”] “Network Error” so it seems to work properly It seems weird to have something different as async await are just wrapper around promises

Having the same problem here. I’m using node 10.7.0, react-native 0.55.4, axios 0.18.0 A failed request from await axios.request(config) triggers catch block, but error argument is undefined. Reverting to .catch(error => onError(error)) works as documented.

Greetings folks! Can y’all please share a full code sample that includes the enclosing function? This totally “works on my machine”:

const axios = require('axios');
const nock = require('nock');

nock.disableNetConnect();

const url = 'http://example';
nock(url).get('/').reply(500);

async function main() {
  try {
    const res = await axios.get(url);
    console.log('Success!');
    console.log(r.status);
    console.log(r.data);
  } catch (e) {
    console.error('Failure!');
    console.error(e.response.status);
  }
}

main().catch(console.error);
beckwith-macbookpro:ax beckwith$ npm start

> ax@1.0.0 start /Users/beckwith/Code/ax
> node server.js

Failure!
500

Also facing this issue:

try {
  const location = await axios.get(`https://api.postcodes.io/postcodes/${location}`);
   ...stuff
} catch (err) {
  errorHandling.onError(err, response);
}

Fails with:

Error: { Error: Request failed with status code 404 [1] at createError (D:\Projects\Chiron\node_modules\axios\lib\core\createError.js:16:15)

What confuses me is that this isn’t caught by the try-catch. Node even says:

(node:9680) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:9680) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It’s like the try catch doesn’t exist…

I am having the same issue here. Two years? Seriously guys…

It seems possible to “cure” this by adding a .catch() to axios/lib/core/dispatchRequest.js:85. Perhaps this is the right approach, since the network events are asynchronous?

Closed due to stale and no one proves with a replicable code sample. If someone persist it is an issue of axios, feel free to open a new issue. Thanks.

It’s been two years since this has been created, has it been fixed? I still have the same issue