handle-callback-err

需要在回调中处理错误

Important

This rule was deprecated in ESLint v7.0.0. Please use the corresponding rule in eslint-plugin-n.

Learn more

在 Node.js 中,处理异步行为的常见模式称为回调模式。此模式需要一个 Error 对象或 null 作为回调的第一个参数。忘记处理这些错误可能会导致你的应用出现一些非常奇怪的行为。

¥In Node.js, a common pattern for dealing with asynchronous behavior is called the callback pattern. This pattern expects an Error object or null as the first argument of the callback. Forgetting to handle these errors can lead to some really strange behavior in your application.

function loadData (err, data) {
    doSomething(); // forgot to handle error
}

规则详情

¥Rule Details

这条规则期望当你在 Node.js 中使用回调模式时,你将处理错误。

¥This rule expects that when you’re using the callback pattern in Node.js you’ll handle the error.

选项

¥Options

该规则采用单个字符串选项:错误参数的名称。默认值为 "err"

¥The rule takes a single string option: the name of the error parameter. The default is "err".

具有默认 "err" 参数名称的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the default "err" parameter name:

在线运行
/*eslint handle-callback-err: "error"*/

function loadData (err, data) {
    doSomething();
}

具有默认 "err" 参数名称的此规则的正确代码示例:

¥Examples of correct code for this rule with the default "err" parameter name:

在线运行
/*eslint handle-callback-err: "error"*/

function loadData (err, data) {
    if (err) {
        console.log(err.stack);
    }
    doSomething();
}

function generateError (err) {
    if (err) {}
}

此规则的正确代码示例以及示例 "error" 参数名称:

¥Examples of correct code for this rule with a sample "error" parameter name:

在线运行
/*eslint handle-callback-err: ["error", "error"]*/

function loadData (error, data) {
    if (error) {
       console.log(error.stack);
    }
    doSomething();
}

正则表达式

¥regular expression

有时(特别是在大型项目中)错误变量的名称在整个项目中并不一致,因此你需要更灵活的配置来确保规则报告所有未处理的错误。

¥Sometimes (especially in big projects) the name of the error variable is not consistent across the project, so you need a more flexible configuration to ensure that the rule reports all unhandled errors.

如果错误变量的配置名称以 ^ 开头,则将其视为正则表达式模式。

¥If the configured name of the error variable begins with a ^ it is considered to be a regexp pattern.

  • 如果选项是 "^(err|error|anySpecificError)$",则规则报告未处理的错误,其中参数名称可以是 errerroranySpecificError

    ¥If the option is "^(err|error|anySpecificError)$", the rule reports unhandled errors where the parameter name can be err, error or anySpecificError.

  • 如果选项为 "^.+Error$",则规则会报告参数名称以 Error 结尾的未处理错误(例如,匹配 connectionErrorvalidationError)。

    ¥If the option is "^.+Error$", the rule reports unhandled errors where the parameter name ends with Error (for example, connectionError or validationError will match).

  • 如果选项为 "^.*(e|E)rr",则规则会报告未处理的错误,其中参数名称与包含 errErr 的任何字符串匹配(例如,将匹配 errerroranyErrorsome_err)。

    ¥If the option is "^.*(e|E)rr", the rule reports unhandled errors where the parameter name matches any string that contains err or Err (for example, err, error, anyError, some_err will match).

何时不使用

¥When Not To Use It

在某些情况下,你的应用忽略错误可能是安全的,但只有在你确信其他形式的监视将帮助你发现问题时才忽略错误。

¥There are cases where it may be safe for your application to ignore errors, however only ignore errors if you are confident that some other form of monitoring will help you catch the problem.

版本

此规则是在 ESLint v0.4.5 中引入。

进阶读物

资源