How to Handle Errors in Node.js ?
Last Updated :
24 Sep, 2024
Node.js is a JavaScript extension used for server-side scripting. Error handling is a mandatory step in application development. A Node.js developer may work with both synchronous and asynchronous functions simultaneously. Handling errors in asynchronous functions is important because their behavior may vary, unlike synchronous functions.
How to Handle Errors in Node?
We have many approaches to handle errors in Node js like try-catch, callback functions, promises, and async-await.
While try-catch blocks are effective for synchronous functions, asynchronous functions can be dealt with callbacks, promises, and async-await. Try-catch is synchronous means that if an asynchronous function throws an error in a synchronous try/catch block, no error throws.
Errors thrown in Node.js applications can be handled in the following ways:
Using try-catch block
The try-catch block can be used to handle errors thrown by a block of code.
JavaScript
function dosomething() {
throw new Error(
'a error is thrown from dosomething');
}
function init() {
try {
dosomething();
}
catch (e) {
console.log(e);
}
console.log(
"After successful error handling");
}
init();
Output:
Error: a error is thrown from dosomething
at dosomething (/home/cg/root/6422736/main.js:4:11)
at init (/home/cg/root/6422736/main.js:9:9)
at Object. (/home/cg/root/6422736/main.js:17:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
After successful error handling
Explanation:
The init() function is called which in turn calls dosomething() function which throws an error object. This error object is caught by the catch block of the init() method. If there is no proper handling of the error the program will terminate. The catch block prints the call stack to show the point where the error occurred.
Using callbacks
A callback is a function called at the completion of a certain task. Callbacks are widely used in Node.js as it prevents any blocking and allows other code to be run in the meantime. The program does not wait for the file reading to complete and proceeds to print “Program Ended” while continuing to read the file. If any error occurs like the file does not exist in the system then the error is printed after “Program Ended”, else the content of the file is outputted.
JavaScript
const fs = require("fs");
fs.readFile('foo.txt', function (err, data) {
if (err) {
console.error(err);
}
else {
console.log(data.toString());
}
});
console.log("Program Ended");
Output:
Program Ended
[Error: ENOENT: no such file or directory,
open 'C:\Users\User\Desktop\foo.txt'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\User\\Desktop\\foo.txt'
}
Explanation:
In this case, the file does not exist in the system hence the error is thrown.
Using promises and promise callbacks
Promises are an enhancement to Node.js callbacks. When defining the callback, the value which is returned is called a “promise”. The key difference between a promise and a callback is the return value. There is no concept of a return value in callbacks. The return value provides more control for defining the callback function. In order to use promises, the promise module must be installed and imported into the application. The .then clause handles the output of the promise. If an error occurs in any .then clause or if any of the promises above are rejects, it is passed to the immediate .catch clause. In case of a promise is rejected, and there is no error handler then the program terminates.
JavaScript
const Promise = require('promise');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost/TestDB';
MongoClient.connect(url)
.then(function (err, db) {
db.collection('Test').updateOne({
"Name": "Joe"
},
{
$set: {
"Name": "Beck"
}
});
})
.catch(error => alert(error.message))
Output:
// In this case we assume the url is wrong
MongoError: failed to connect to server [localhost:27017]
// error message may vary
Using async-await
Async-await is a special syntax to work with promises in a simpler way that is easy to understand. When we use async/await, .then is replaced by await which handles the waiting in the function. The error handling is done by the .catch clause. Async-await can also be wrapped in a try-catch block for error handling. In case no error handler exists the program terminates due to an uncaught error.
JavaScript
async function f() {
let response = await fetch('http://xyzurl');
}
// f() becomes a rejected promise
f().catch(alert);
Output:
// the url is wrong //
TypeError: failed to fetch
Similar Reads
How to Handle Syntax Errors in Node.js ?
If there is a syntax error while working with Node.js it occurs when the code you have written violates the rules of the programming language you are using. In the case of Node.js, a syntax error might occur if you have mistyped a keyword, or if you have forgotten to close a parenthesis or curly bra
4 min read
How to handle errors in React?
Handling errors in React is essential for creating a smooth user experience and ensuring the stability of your application. Whether you're working with functional or class components, React provides different mechanisms to handle errors effectively. 1. Using Error BoundariesError boundaries are a fe
5 min read
How to handle error in Express.js ?
In this article, we will learn about the error handling process in Express.js and how it is done. Error handling process in Express.js refers to the condition when errors occur in the execution of the program which may contain a continuous or non-continuous type of program code. The error handling i
6 min read
How to Handle Errors in JavaScript ?
In JavaScript Error Handling is used to find the errors in the code so, that the coder can find the particular issue to resolve it. Here are the different approaches to handle errors in JavaScript 1. Using try catch and finally statementThe try, catch, and finally blocks are used for error handling.
4 min read
How to handle errors in Promise.all ?
To handle errors in Promise.all(), you need to understand that it fails immediately if any of the promises reject, discarding all resolved values. This behavior can lead to incomplete operations and potential application crashes. The best way to manage errors in Promise.all() is by using .catch() in
3 min read
How to Handle MySQL Connection Errors in NodeJS?
Dealing with MySQL connection errors requires you to look at issues related to establishing, maintaining, and closing connections to the MySQL database. This includes initial connection failure, connection drop detection and recovery, and error handling during query execution. Effective error handli
2 min read
How to Avoid Callback Hell in Node.js ?
Callback hell, often referred to as "Pyramid of Doom," occurs in Node.js when multiple nested callbacks lead to code that is hard to read, maintain, and debug. This situation arises when each asynchronous operation depends on the completion of the previous one, resulting in deeply nested callback fu
3 min read
How to Use Handle Get Request in Express.js ?
Express.js is a popular web application framework for Node.js, known for its simplicity and flexibility. One of the fundamental tasks in web development is handling HTTP requests, and GET requests are among the most common. This article will guide you through the process of handling GET requests in
3 min read
How to Handle Errors in MongoDB Operations using NodeJS?
Handling errors in MongoDB operations is important for maintaining the stability and reliability of our Node.js application. Whether we're working with CRUD operations, establishing database connections, or executing complex queries, unexpected errors can arise. Without proper error handling, these
8 min read
How to read and write files in Node JS ?
NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada
2 min read