DEV Community

codeagboro
codeagboro

Posted on

Handling exception in Node Js Express

Node.js is a powerful platform for building server-side applications, but like all programming languages, it can be prone to errors and exceptions. These exceptions can arise due to miscellaneous factors such as incorrect input, external dependencies, or network issues. This article discusses the handling of Node.js and Express exceptions – one of the most popular web application frameworks built on Node.js.

An exception is an event that disrupts the normal flow of a program's instructions, which then returns an error message when it occurs. In Node.js, whenever code confronts an issue that it cannot handle, an exception is thrown which is then sent to the runtime system in search of an appropriate handler.

Types of Exceptions

Synchronous
A synchronous exception is an exception that takes place while a program is in the middle of running. If a synchronous exception occurs, it halts the execution of the program and directs it to the closest catch block which can manage it.
As an illustration, take a look at this code snippet:

function divide(x, y) {
  if (y === 0) {
    throw new Error('Division by zero!');
  }
  return x / y;
}

try {
  const result = divide(10, 0);
  console.log(result);
} catch (error) {
  console.log(error.message);
}
Enter fullscreen mode Exit fullscreen mode

When we call the divide function with arguments (10, 0), an exception is thrown immediately which forces the execution to jump to the catch block where it logs an error message, 'Division by zero!'. Synchronous exceptions can be easy to handle and their predictable nature makes them desirable in some cases. However, if not handled properly they can cause abrupt termination of a program which is undesirable. To minimize this risk, one can employ defensive programming techniques such as validating inputs and handling edge cases. Thus, understanding how to handle synchronous exceptions is crucial for any developer. By using defensive programming and proper error handling mechanisms, we can make our programs more reliable and robust by minimizing any synchronous exceptions that may occur.

Asynchronous
When dealing with asynchronous code, errors can be thrown. This is known as an asynchronous exception, and won't be handled immediately - the program continues execution until it reaches the nearest error handler. In Node.js and Express, there are two ways we can handle such exceptions: using the try-catch mechanism, or employing error middleware.

Using try-catch allows us to wrap our asynchronous code in a try block, and specify a catch block for any exceptions thrown. For example:

router.get('/', async (req, res) => {
  try {
    const data = await fetchData();
    res.send(data);
  } catch (error) {
    console.log(error);
    res.status(500).send('Internal Server Error');
  }
});
Enter fullscreen mode Exit fullscreen mode

In this code, we have an asynchronous route handler that's calling the fetchData function which returns a Promise. We attempt to call it by establishing a try block and catching any exceptions that might be thrown in the catch block. If an issue arises, we log the error and send a 500 Internal Server Error response back to the user.

An alternate way to deal with asynchronous exceptions is to utilize error middleware. Error middleware catches any unforeseen exceptions and sends an appropriate error response back to the user. For example:

function errorHandler(err, req, res, next) {
  console.log(err);
  res.status(500).send('Internal Server Error');
}

app.use(errorHandler);
Enter fullscreen mode Exit fullscreen mode

This code defines an error handler middleware function which takes four arguments: err, req, res and next. When an unhandled exception occurs in our application, this function is invoked by the system and logs the error message to the console. Additionally, a 500 Internal Server Error response is sent back to the client.

It is essential that errors are handled correctly as this will improve robustness and reliability of both Node.js and Express applications, as well as providing an excellent user experience.

In conclusion, handling exceptions should be a priority in order for applications to remain stable; however it is just as important to take steps in order to prevent them from arising in the first place by writing error-free code, validating inputs and using external libraries responsibly.

Top comments (0)