Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
Is it a good idea to handle all requests and responses (server side) and all API calls (frontend side) using try-catch blocks?

I have a MERN Application and I am making API calls in the frontend using axios.

For example:

 // user is an object with properties email and password
    const res = await axiosInstance.post("/admin/sign-in", {
            email: user.email,
            password: user.password,
          });

If the API call fails (for whatever reason) I get an error, so is it a good idea to always make API calls in a try-catch block. If not, what is the recommended way of doing so? I know it is possible to check the response status, but is it an improper practice to use try-catch? The status check does not work in case the server is down.

    try {
      const res = await axiosInstance.post("/admin/sign-in", {
        email: user.email,
        password: user.password,
      });
} catch (error) {
      console.log(error);
    }

Similarly, on the backend side, should I handle all requests and responses in a try-catch block so that an accidental (or deliberate ๐Ÿ˜‰) misplaced JSON token in the request does not cause an error that shuts down the server?

Thank you.

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

I prefer to throw typed errors from my handlers, and not catch them there; Either an internal system error or and API error with a message for the client.

Then I catch the error higher up in the stack, in the default error handler, sending an API message to the user, and/or logging the system message internally for internal errors, depending on the error type. This way I can catch all errors as well, eliminating boilerplate code.

This also allows me to consolidate, and control what gets sent to the client.

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!