Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda