简体   繁体   中英

How to get the line number of error inside of catch on ajax call in javascript

When I use try, catch I can get line number of error by stack message.
But When I use this for ajax call as below,

try {
 await axios.get('http://example.com')
} catch (error) {
 console.log(error)
}

I can't get the stack error. It just gives me error of ajax call.
Isn't there any method for catching the line number in this case?
Thank you for reading it.

Usually, error will contain enough information to identify the problem. error is an object, when you do console.log(error) , it actually calls its toString implementation.

To get the stack only, you can use.

try {
    throw new Error('Test');
} catch (error) {
    console.log(error.stack);
}

The above will only point to the JavaScript file/piece of code that fires the request to example.com , nothing more. :)

If you want to see what happened with your call (more details of why it failed), and what was the response, try console.log(JSON.stringify(error)) and use Network DevTools -https://developers.google.com/web/tools/chrome-devtools/network

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM