1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
# node/handle-callback-err
> require error handling in callbacks
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.
```js
function loadData (err, data) {
doSomething(); // forgot to handle error
}
```
## 📖 Rule Details
This rule expects that when you're using the callback pattern in Node.js you'll handle the error.
### Options
The rule takes a single string option: the name of the error parameter. The default is `"err"`.
Examples of **incorrect** code for this rule with the default `"err"` parameter name:
```js
/*eslint handle-callback-err: "error"*/
function loadData (err, data) {
doSomething();
}
```
Examples of **correct** code for this rule with the default `"err"` parameter name:
```js
/*eslint handle-callback-err: "error"*/
function loadData (err, data) {
if (err) {
console.log(err.stack);
}
doSomething();
}
function generateError (err) {
if (err) {}
}
```
Examples of **correct** code for this rule with a sample `"error"` parameter name:
```js
/*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.
* If the option is `"^(err|error|anySpecificError)$"`, the rule reports unhandled errors where the parameter name can be `err`, `error` or `anySpecificError`.
* If the option is `"^.+Error$"`, the rule reports unhandled errors where the parameter name ends with `Error` (for example, `connectionError` or `validationError` will match).
* 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).
## 🔎 Implementation
- [Rule source](../../lib/rules/handle-callback-err.js)
- [Test source](../../tests/lib/rules/handle-callback-err.js)
|