最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can some one explain what are the error codes in nodejs (not status codes) - Stack Overflow

programmeradmin5浏览0评论

This question I faced in my Interviews many times, but still I can't find the answer for this. I already googled it, but did not find the exact answer for this. Pls anybody let me know the exact answer for this.

Question: What are Error Codes in NodeJs?

I answered the error status codes. But the Interviewer said that, am not asking status codes. Pls explain the answer. Thank you in advance.

This question I faced in my Interviews many times, but still I can't find the answer for this. I already googled it, but did not find the exact answer for this. Pls anybody let me know the exact answer for this.

Question: What are Error Codes in NodeJs?

I answered the error status codes. But the Interviewer said that, am not asking status codes. Pls explain the answer. Thank you in advance.

Share Improve this question edited May 11, 2020 at 17:36 Navod Janitha 435 bronze badges asked Feb 20, 2019 at 10:50 vishnu karthik Reddyvishnu karthik Reddy 1741 gold badge4 silver badges14 bronze badges 1
  • 3 FYI nodejs/api/errors.html#errors_error_code and nodejs/api/errors.html#nodejs-error-codes and – marekful Commented Feb 20, 2019 at 10:53
Add a ment  | 

2 Answers 2

Reset to default 8

You are facing this question because error code was introduced in node 8.x previous to 8 there was error message in Error class which caused issues in very basic things due to which developers was forced to create custom classes for Error. Refer https://nodejs/docs/latest-v7.x/api/errors.html#errors_class_error

Later in Node 8, Error Codes are introduced in node 8.x to all of the error objects thrown by the Node.js APIs https://nodejs/api/errors.html#errors_class_error

Node Error Codes: https://nodejs/dist/latest/docs/api/errors.html#errors_node_js_error_codes

Why you should use error codes?

Till Node7, most of the Errors thrown by Node.js only had a message associated with them. If you wanted to have your code take a specific action based on the Error, you would have had to pare the message string to a known value. The result might be something like:

try {
// Do something
}
catch(error) {
    if (error.message == 'a simpe error'){
        // do something
    }
    else {
        // do something
    }
}

This is not good practice as in most cases when you get an error from Node.js it’s more likely that your code will simply log/display the message and then branch to a mon recovery path.

Now you might have noticed that there is a typo in the message parison used in the example above. Well, that can happen with the Node.js code base as well or there are possibilities that node redefine the message strings.

The hard dependency on the message string also poses a challenge for internationalization.

This should allow the earlier example to be re-written as follows:

try {
    // Do something
}
catch(error) {
    if (error.code == 'A_ERROR_CODE'){
        // do something
    }
    else {
        // do something
    }
}

Using above practice will ensure that if/when the message changes in the future, the code won’t be affected as the error code remains unchanged.

Yes error codes & status codes are different things. Error codes are whenever we have any ever in node like

try{} catch(error){ error.code }

these are error codes. For more detail kindly check this blog on medium Medium Blog

发布评论

评论列表(0)

  1. 暂无评论