Axios is it slower to auto catch error responses such as 404

0 votes
Just out of curiosity, the default setting for the axios package in javascript, node, or any other language is to throw an error for any response 400 or larger (I'm not sure if that's precisely correct, but meh). I've learned in the past that it's generally bad practise to use catch when an if statement would have sufficed. In order to answer my issue, would it be preferable to use an if statement rather than a catch in situations where 404 errors are anticipated to occur frequently (for the package axios, this entails changing the default settings for validateStatus)?

Things to think about: Does using AWS Lambda make a difference? Is it different for various JavaScript/browser/Node versions?

I would appreciate any advice:
Feb 14, 2023 in AWS by Ashwini
• 5,430 points
834 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

In general, using a catch statement in Axios to handle errors is not slower than using an if statement to check for errors, as the difference in performance is likely to be negligible. In fact, using a catch statement can be more convenient and efficient, especially if you are handling multiple errors and want to avoid writing repetitive code.

However, if you expect to encounter a specific error response code (such as a 404) frequently and want to handle it differently than other errors, it might make more sense to customize the Axios instance using the validateStatus option. This option allows you to define a custom function that determines whether a given HTTP response status code is considered an error or not. By default, Axios considers any status code outside the range of 200-299 to be an error.

For example, to treat 404 responses as non-errors, you could set validateStatus as follows:

axios.create({
  validateStatus: function (status) {
    return status >= 200 && status < 300 || status === 404; // status >= 200 and < 300 or status is 404
  }
});

As for whether using AWS Lambda or different JavaScript/browser/Node versions would make a difference, it's unlikely to have a significant impact on the choice of using a catch statement or an if statement to handle errors in Axios. However, performance considerations for your specific use case may vary based on the specific environment you are working in.

answered Feb 16, 2023 by anonymous

edited Mar 5

Related Questions In AWS

+2 votes
3 answers

Is it possible to ping AWS EC2 instance

1.Go to EC2 Dashboard and click "Running ...READ MORE

answered Nov 13, 2020 in AWS by shivam
• 140 points

edited Nov 13, 2020 by Gitika 21,019 views
0 votes
1 answer

Is it possible to stop nodes in AWS ElastiCache cluster

At this time there is not a ...READ MORE

answered Jun 21, 2018 in AWS by Flying geek
• 3,280 points
2,292 views
0 votes
1 answer

Nodejs Instagram videos downloader not working

Firstly, Initialize a package.json file with default values. npm ...READ MORE

answered Jun 9, 2022 in Node-js by Neha
• 9,020 points
1,505 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
2,067 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,604 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP