]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: handle-callback-err | |
8f9d1d4d DC |
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 | ||
eb39fafa | 9 | |
f2a92ac6 | 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). |
56c4a2cb | 11 | |
eb39fafa DC |
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 | ||
8f9d1d4d DC |
32 | ::: incorrect |
33 | ||
eb39fafa DC |
34 | ```js |
35 | /*eslint handle-callback-err: "error"*/ | |
36 | ||
37 | function loadData (err, data) { | |
38 | doSomething(); | |
39 | } | |
40 | ||
41 | ``` | |
42 | ||
8f9d1d4d DC |
43 | ::: |
44 | ||
eb39fafa DC |
45 | Examples of **correct** code for this rule with the default `"err"` parameter name: |
46 | ||
8f9d1d4d DC |
47 | ::: correct |
48 | ||
eb39fafa DC |
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 | ||
8f9d1d4d DC |
64 | ::: |
65 | ||
eb39fafa DC |
66 | Examples of **correct** code for this rule with a sample `"error"` parameter name: |
67 | ||
8f9d1d4d DC |
68 | ::: correct |
69 | ||
eb39fafa DC |
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 | ||
8f9d1d4d DC |
81 | ::: |
82 | ||
eb39fafa DC |
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. |