Skip to content
Related Articles

Related Articles

Improve Article

Node.js util.getSystemErrorName() Method

  • Last Updated : 08 Oct, 2021
Geek Week

The util.getSystemErrorName() method is defined in utilities module of Node.js standard library. It is used to know the type of error that occurs in the program. Generally, this method is used within some other method to know if that method does not give response as expected because some error occurs then what type of error that was to ensure that our program does break.

Syntax

util.getSystemErrorName( err )

Parameters: This method accepts a single parameter ‘err’ that holds a numerical value that specifies the error number or error code. This error code comes from Node.js API itself.

Return Value: Returns a string name i.e. error name for a numeric error code.

Note: There are some common system error that are mapped to specific error code. The mapping between system error and error code is platform dependent. Some common system errors are shown below.



  • EACCES(Permission denied): This Error occurs when a file was trying to access whose access permission is forbidden.
  • EADDRINUSE(Address already in use): This occurs when an attempt to bind a server (net, http, or https) to a local address failed due to another server on the local system already occupying that address.
  • ECONNRESET(Connection reset by peer): This occurs when a connection was forcibly closed by a peer. This normally results from a loss of the connection on the remote socket due to a timeout or reboot.
  • EISDIR(Is a directory): This occurs when an operation expected a file, but the pathname of directory is given.
  • EMFILE(Too many open files in system): This occurs when the maximum number of file descriptors allowable on the system has been reached, and requests for another descriptor cannot be fulfilled until at least one has been closed. This is encountered when opening many files at once in parallel.
  • ENOENT(No such file or directory): This error is commonly raised by fs operation when the component of the specified pathname does not exist i.e. No entity (file or directory) could be found by the given path.
  • ENOTDIR(Not a directory): This occurs when a component of the given pathname existed, but was not a directory.
  • ENOTEMPTY(Directory not empty): This occurs when the requested directory is not empty as expected.
  • TIMEDOUT(Operation timed out): this occurs when a connection is established and the request is sent but failed because the connected party did not properly respond after a period of time.

Example 1:




// Importing File System module
const fs = require('fs')
  
// Importing utilities module
const util = require('util')
  
fs.readdir('file/not/exist', (err, files) => {
  if(err){
    const errName = util.getSystemErrorName(err.errno)
    console.log(`Error Name --> ${errName}`)
    console.log(`Error Code --> ${err.errno}`)
  }else{
    for(let file in files){
      console.log(file)
    }
  
}) 

Output:

Error Name --> ENOENT
Error Code --> 4058

Explanation: Program is basically to fetch all the files and folders in a given directory. The fs.readdir() method takes path to the target directory. In case, if the given path is not valid then an error occurs which may break our program and so have to handle the error and provide the user some valid output so that the user aware of why an error occurs. Here to handle the error getSystemErrorName() method is used which returns the name of the error that occurs.

Example 2:




// Importing File System module
const fs = require('fs')
  
// Importing utilities module
const util = require('util')
  
fs.readdir('./index.js', (err, files) => {
  if(err){
    const errName = util.getSystemErrorName(err.errno)
    console.log(`Error Name --> ${errName}`)
    console.log(`Error Code --> ${err.errno}`)
  }else{
    for(let file in files){
      console.log(file)
    }
  
}) 

Output:

Error Name --> ENOTDIR
Error Code --> 4052

Explanation: Program is basically to fetch all the files and folders in a given directory. The fs.readdir() method takes the path to the target directory. Since the given path is not a directory but a file, an error named ENOTDIR occurs.




My Personal Notes arrow_drop_up
Recommended Articles
Page :