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