简体   繁体   English

有人可以解释一下nodejs中的错误代码是什么(不是状态代码)

[英]Can some one explain what are the error codes in nodejs (not status codes)

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?问题:NodeJ 中的错误代码是什么?

I answered the error status codes.我回答了错误状态代码。 But the Interviewer said that, am not asking status codes.但是面试官说,我不是在问状态码。 Pls explain the answer.请解释答案。 Thank you in advance.先感谢您。

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.您面临这个问题是因为在 8.x 之前的节点 8.x 中引入了错误代码,Error 类中有错误消息,导致非常基本的事情出现问题,因为开发人员被迫为 Error 创建自定义类。 Refer https://nodejs.org/docs/latest-v7.x/api/errors.html#errors_class_error参考https://nodejs.org/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.org/api/errors.html#errors_class_error后来在 Node 8 中,Node 8.x 中向 Node.js API 抛出的所有错误对象引入了错误代码https://nodejs.org/api/errors.html#errors_class_error

Node Error Codes: https://nodejs.org/dist/latest/docs/api/errors.html#errors_node_js_error_codes节点错误代码: https : //nodejs.org/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.直到 Node7,Node.js 抛出的大多数 Errors 都只有一条消息与之关联。 If you wanted to have your code take a specific action based on the Error, you would have had to compare 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 common recovery path.这不是一个好的做法,因为在大多数情况下,当您从 Node.js 收到错误时,您的代码更有可能只是简单地记录/显示消息,然后分支到公共恢复路径。

Now you might have noticed that there is a typo in the message comparison 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.好吧,Node.js 代码库也可能发生这种情况,或者节点可能会重新定义消息字符串。

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有关更多详细信息,请查看中型博客上的此博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM