Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is the ifError method of the assert module in Node.js?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ifError method of the assert module in Node.js throws an error if the provided value is not null or undefined. The ifError method is commonly used to test error arguments in callbacks.

The process is illustrated below:

To use the ifError method, you will need to install the assert module using the command prompt, as shown below:

npm install assert

After the installation is complete, you will need to import the assert module into the program, as shown below:

const assert = require('assert');

The prototype of the ifError method is shown below:

ifError(value)

Paramaters

The ifError method takes a single mandatory parameter that represents the value to be checked for an error.

Return value

If value is not null or undefined, then the ifError method throws an AssertionError, and the program terminates; otherwise, execution continues as normal.

Example

The code below shows how the ifError method works in Node.js:

const assert = require('assert');
// evaluating first expression
try{
assert.ifError('23')
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating second expression
try{
assert.ifError('error')
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating third expression
try{
assert.ifError(null)
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating fourth expression
try{
assert.ifError(undefined)
console.log("No error.")
}
catch(error){
console.log(error.message)
}

Explanation

The code above uses 44 different expressions to show the behavior of the ifError method.

In the first two expressions in lines 55 and 1414, value is “23” and “error”, respectively. Since value is neither null nor undefined, the ifError method throws an error, which triggers the catch branch of the try-catch block. The code outputs the message associated with the error, i.e., the value parameter itself.

In the expressions in line 2323 and 3232, value is null and undefined, respectively. Therefore, the ifError method does not throw any errors, and only the try branch of the try-catch block executes.

RELATED TAGS

node.js

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring