handle-callback-err
此规则在 ESLint v7.0.0 中已弃用。请使用 eslint-plugin-node
中的相应规则。
在 Node.js 中,处理异步行为的常见模式称为回调模式。此模式需要一个 Error
对象或 null
作为回调的第一个参数。忘记处理这些错误可能会导致您的应用程序出现一些非常奇怪的行为。
function loadData (err, data) {
doSomething(); // forgot to handle error
}
规则详情
这条规则期望当您在 Node.js 中使用回调模式时,您将处理错误。
选项
该规则采用单个字符串选项:错误参数的名称。默认值为 "err"
。
使用默认 "err"
参数名称的此规则的错误代码示例:
/*eslint handle-callback-err: "error"*/
function loadData (err, data) {
doSomething();
}
dxPlFhz0xhJXtLyeY6Wes3Ju4MOgLQRjBcCuwvovAm7l7E585Fah+r6djoU+SMeGoteOSher3lzfwX0OJ5w0g3nHlejKgSq1mXHZh4xMJS8PhoTX0kDQWb0bW97Kn07v
/*eslint handle-callback-err: "error"*/
function loadData (err, data) {
if (err) {
console.log(err.stack);
}
doSomething();
}
function generateError (err) {
if (err) {}
}
PwR5DrJrN80min96hKNCvaJI/5PvcSQAJ4mf+0w8V9i+76PxNfUUf1qw3ST710KhTezyNeCPuKviHVPLmIIXAWrQr41vPnRMi3LHT5a5pZs=
/*eslint handle-callback-err: ["error", "error"]*/
function loadData (error, data) {
if (error) {
console.log(error.stack);
}
doSomething();
}
regular expression
o+mFinVtdN9YmEzsfjXLj4wLdi/pHyYy2dKT8LIgdcpMqb4ZUTC89Qu9QrmXVwZElBm74K7k0Hf0RsQH+qUg8v3o9nI/GVRYq1HZEeyyiid7g6Vud0JvUbLcysAQVfRdHSy7vwM7ABUwYZyCNtTtzZEo/XeuQ8Ob2rWmNuVbLd7c3CZgOYgYC5/7bhIgqRtYMj7/vy86+rXd02QcMZwjc1MPEVLgekCpsqsi4D+G1XM=
NJ64jnAT4xCLVico4K7vZSLagIp2czPx2t0fmMwvk5nhi1BSuwBbQG14QwZC7vUdq95IvEje2BlrenJXVyE7HWdA3I9tpkMIrredQorGSIWhBmz2i1kbhIXrZ//3iU3vdh7oBQ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