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