Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Latest commit

 

History

History
81 lines (65 loc) · 2.84 KB

handleCallbackErrRule.md

File metadata and controls

81 lines (65 loc) · 2.84 KB

handle-callback-err (ESLint: handle-callback-err)

rule_source test_source

enforce error handling in callbacks

Rationale

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.

Config

The rule takes a string option: the name of the error parameter. The default is "err".

Sometimes 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).

In addition to the string we may specify an options object with the following property:

  • allowProperties: (true by default) When this is set to false the rule will not report unhandled errors as long as the error object is handled without accessing any of its properties at least once. For instance, (err) => console.log(err.stack) would report an issue when allowProperties is set to false because err is not handled on its own.

Examples

"handle-callback-err": [true, "error"]
"handle-callback-err": [true, "^(err|error|anySpecificError)$"]
"handle-callback-err": [true, { "allowProperties": false }]
"handle-callback-err": [true, "^(err|error|anySpecificError)$", { "allowProperties": false }]

Schema

{
  "type": "array",
  "items": [
    {
      "type": "string"
    },
    {
      "type": "object",
      "properties": {
        "allowProperties": "boolean"
      },
      "additionalProperties": false
    }
  ],
  "minLength": 0,
  "maxLength": 2
}