Skip to content

Latest commit

 

History

History
87 lines (74 loc) · 3.24 KB

error_handling.md

File metadata and controls

87 lines (74 loc) · 3.24 KB

Error Handling in Node.js

Overview

Proper error handling is crucial for maintaining the reliability and robustness of Node.js applications. This document explores best practices for handling errors in various scenarios, including synchronous and asynchronous operations.

1. Basic Error Handling

Node.js supports several methods for handling errors, including try-catch blocks for synchronous code and callbacks for asynchronous operations.

Example: Synchronous Error Handling

try {
    // Code that may throw an error
    let result = riskyFunction();
    console.log(result);
} catch (error) {
    console.error('Error encountered:', error);
}

This snippet uses a try-catch block to handle errors that may occur in synchronous code.

2. Asynchronous Error Handling

Asynchronous operations require different error handling strategies, such as callbacks, promises, and async/await.

Callbacks

fs.readFile('path/to/file', (err, data) => {
    if (err) {
        console.error('Failed to read file:', err);
        return;
    }
    console.log('File data:', data);
});

In this example, errors in the asynchronous readFile operation are handled within the callback.

Promises

fetch('http://example.com/data')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error('Failed to fetch data:', error));

Promises allow chaining .catch() to handle any errors that occur during the promise execution chain.

Async/Await

async function fetchData() {
    try {
        let response = await fetch('http://example.com/data');
        let data = await response.json();
        console.log(data);
    } catch (error) {
        console.error('Failed to fetch data:', error);
    }
}

Async/await makes handling errors in asynchronous operations more intuitive with try-catch blocks.

3. Using Error-Handling Middleware in Express

In Express, you can use middleware to handle errors uniformly across your application.

Example: Express Error-Handling Middleware

app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

This middleware function catches errors in the app and sends a generic 500 error response.

4. Handling Uncaught Exceptions

Uncaught exceptions can be captured using the uncaughtException event.

Example: Process on UncaughtException

process.on('uncaughtException', (error) => {
    console.error('Uncaught Exception:', error);
    // Gracefully shut down the application, restart is usually required
    process.exit(1);
});

This event handler logs the error and exits the process, which is often necessary to recover from an uncaught exception.

Conclusion

Effective error handling in Node.js involves anticipating potential failures and implementing strategies to deal with these errors gracefully. By utilizing callbacks, promises, async/await, and middleware effectively, you can ensure that your application handles errors robustly and maintains a high level of reliability.

For more in-depth practices and error handling patterns, refer to the Node.js official documentation on error handling.