]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/handle-callback-err.md
4f4b8c955077ef78eb1179affb0ad786feeb8f0d
[pve-eslint.git] / eslint / docs / src / rules / handle-callback-err.md
1 ---
2 title: handle-callback-err
3 layout: doc
4 rule_type: suggestion
5 further_reading:
6 - https://github.com/maxogden/art-of-node#callbacks
7 - https://web.archive.org/web/20171224042620/https://docs.nodejitsu.com/articles/errors/what-are-the-error-conventions/
8 ---
9
10
11 This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-node`](https://github.com/mysticatea/eslint-plugin-node).
12
13 In Node.js, a common pattern for dealing with asynchronous behavior is called the callback pattern.
14 This pattern expects an `Error` object or `null` as the first argument of the callback.
15 Forgetting to handle these errors can lead to some really strange behavior in your application.
16
17 ```js
18 function loadData (err, data) {
19 doSomething(); // forgot to handle error
20 }
21 ```
22
23 ## Rule Details
24
25 This rule expects that when you're using the callback pattern in Node.js you'll handle the error.
26
27 ## Options
28
29 The rule takes a single string option: the name of the error parameter. The default is `"err"`.
30
31 Examples of **incorrect** code for this rule with the default `"err"` parameter name:
32
33 ::: incorrect
34
35 ```js
36 /*eslint handle-callback-err: "error"*/
37
38 function loadData (err, data) {
39 doSomething();
40 }
41
42 ```
43
44 :::
45
46 Examples of **correct** code for this rule with the default `"err"` parameter name:
47
48 ::: correct
49
50 ```js
51 /*eslint handle-callback-err: "error"*/
52
53 function loadData (err, data) {
54 if (err) {
55 console.log(err.stack);
56 }
57 doSomething();
58 }
59
60 function generateError (err) {
61 if (err) {}
62 }
63 ```
64
65 :::
66
67 Examples of **correct** code for this rule with a sample `"error"` parameter name:
68
69 ::: correct
70
71 ```js
72 /*eslint handle-callback-err: ["error", "error"]*/
73
74 function loadData (error, data) {
75 if (error) {
76 console.log(error.stack);
77 }
78 doSomething();
79 }
80 ```
81
82 :::
83
84 ### regular expression
85
86 Sometimes (especially in big projects) the name of the error variable is not consistent across the project,
87 so you need a more flexible configuration to ensure that the rule reports all unhandled errors.
88
89 If the configured name of the error variable begins with a `^` it is considered to be a regexp pattern.
90
91 * If the option is `"^(err|error|anySpecificError)$"`, the rule reports unhandled errors where the parameter name can be `err`, `error` or `anySpecificError`.
92 * If the option is `"^.+Error$"`, the rule reports unhandled errors where the parameter name ends with `Error` (for example, `connectionError` or `validationError` will match).
93 * 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).
94
95 ## When Not To Use It
96
97 There are cases where it may be safe for your application to ignore errors, however only ignore errors if you are
98 confident that some other form of monitoring will help you catch the problem.