• Node.js Video Tutorials

Node.js - assert.ifError() Function



The Node.js assert.ifError() function is used to test for errors in asynchronous operations and raise an error if one occurs.

It takes one argument, which should be either an Error object or a value that evaluates to false (such as undefined or null). If the argument is undefined or, null, it throws an error with the provided message. Otherwise it does nothing and returns undefined. This allows developers to write code that handles both synchronous and asynchronous errors without having to manually check for them each time.

Syntax

Following is the syntax of the Node.js assert.ifError() function

assert.ifError(value);

Parameters

  • value − (required) The value we passed is stored in this parameter. This function throws this value if it is not undefined or null and the Input value can be any type.

Return Value

This function returns the AssertionError of the object type to output.

Example

In the following example below, we are passing 'undefined' to the value parameter of the Node.js assert.ifError() function.

const assert = require('assert');
var und = undefined;
assert.ifError(und);

Output

On executing the above program, the ifError() function will not throw AssertionError because the value is undefined.

// Returns nothing

Example

In the example below, we are passing 'null' to the value parameter of the Node.js assert.ifError() function.

const assert = require('assert');
var NULL = null;
assert.ifError(NULL);

Output

On executing the above program, the ifError() function will not throw AssertionError because the value is null.

// Returns nothing

Note − The function will throw AssertionError along with the value unless the value is undefined or null.

Example

In the example below, we are passing an integer to the value parameter of the function.

const assert = require('assert');
var num = 55;
assert.ifError(num);

Output

On executing the above program, the function will throw AssertionError along with the value we passed.

assert.js:667
   throw newErr;
   ^
   
AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 55
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:4:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the following example below, we are passing a string to the value parameter of the function.

const assert = require('assert');

var text = 'Tutorialspoint';
assert.ifError(text);

Output

On executing the above program, the function will throw AssertionError along with the value we passed.

assert.js:667
   throw newErr;
   ^
   
AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'Tutorialspoint'
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:4:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the following example below, we are passing an instance of an Error to the message parameter of the function.

const assert = require('assert');
var err = new Error('Error warning!!!!');
assert.ifError(err);

Output

So when we compile and run the code, the function will throw AssertionError and the value of Error will be thrown.

assert.js:667
   throw newErr;
   ^
   
AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error warning!!!!
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:4:8)
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:3:11)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
nodejs_assert_module.htm
Advertisements