Node.js – util.getSystemErrorName() Method


The util.getSystemErrorName() method is used for returning the string name of the numeric error code that is returned from the Node.js API. There is a mapping between the error codes and error names which is platform-dependent.

Syntax

util.getSystemErrorName(err)

Parameters

It takes a single parameter −

  • err −This parameter takes input as a numeric value that will specify the error number or the error code.

The function returns the error name based upon the error code or error number passed in the err parameter.

Some common system errors are - EACCES, EEXIST, EISDIR, ENOENT, ENOTDIR, ENOTEMPTY, etc. Browse this page to find the complete information about system error codes.

Example 1

Create a file with the name "systemErrorName.js" and copy the following code snippet. After creating the file, use the command "node systemErrorName.js" to run this code.

// util.getSystemErrorName() Demo Example

// Importing v8 module
const util = require('util');
const fs = require('fs');

var location = 'file/not/exist';
fs.access(location, (err) => {
   // Print error if file does not exists
   const name = util.getSystemErrorName(err.errno);
   // Printing the error name from mapping
   console.error(name);
});

Output

C:\home
ode>> node systemErrorName.js ENOENT

Example 2

Let’s have a look at one more example

// util.getSystemErrorName() Demo Example

// Importing v8 module
const util = require('util');
const fs = require('fs');

// Checking if directory exists inside this file
fs.readdir('./index.js', (err) => {
   // Print error if directory does not exists
   const name = util.getSystemErrorName(err.errno);
   // Printing the error name from mapping
   console.error('Error Name: ', name);
   console.error('Error Code: ', err.errno);
});

Output

C:\home
ode>> node systemErrorName.js Error Name: ENOTDIR Error Code: -20

Updated on: 18-Aug-2021

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements