j10
j10

Reputation: 2271

node.js extract error meaning from error.code and error.errno

I am using child process in Node.js and when I try to spawn a process for which executable does not exist the spawn throws a error object : https://nodejs.org/api/errors.html#errors_class_system_error

with error.errno and error.code as "ENOENT" which means "no such files or directory exist"

but I need its meaning that is "no such files or directory exist" which is not available in error object Is there a class in Node.js which can help me with the same.

Upvotes: 1

Views: 12545

Answers (1)

auroranil
auroranil

Reputation: 2661

Input the system error code into this function and it will give you the description of the error.

function system_error_description(err_code) {
  if(typeof err_code != "string"
      || err_code.length < 2 
      || err_code[0] != "E") {
    return "Invalid system error code '" + err_code.toString() + "'";
  }
  switch(err_code) {
    case "EACCES":
      return "Permission denied";
    case "EADDRINUSE":
      return "Address already in use";
    case "ECONNREFUSED":
      return "Connection refused";
    case "ECONNRESET":
      return "Connection reset by peer";
    case "EEXIST":
      return "File exists";
    case "EISDIR":
      return "Is a directory";
    case "EMFILE":
      return "Too many open files in system";
    case "ENOENT":
      return "No such file or directory";
    case "ENOTDIR":
      return "Not a directory";
    case "EPERM":
      return "Operation not permitted";
    case "EPIPE":
      return "Broken pipe";
    case "ETIMEDOUT":
      return "Operation timed out";
    default:
      return "System error code '" + err_code + "' not recognized";
  }
}

Upvotes: 1

Related Questions