Socket
Book a DemoInstallSign in
Socket

eslint-plugin-no-catch-all

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-no-catch-all

prevent catch blocks that don't rethrow unexpected errors

1.1.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

eslint-plugin-no-catch-all

It is very easy to accidentally swallow errors with catch blocks, that should just crash because they are a simple developer error. Consider this case for example.

try {
  someFunctionWhichMayThrowSpecificError();
  // a stupid mistake
  let obj;
  if (obj.prop === "something") {
    // do something
  }
} catch (e) {
  // code to handle one specific error case
}

This error swallowing can be a real pain and lead to hard to debug issues that would otherwise be trivial to catch. Instead the catch block should always try to identify the specific error by checking for some specific prop that is set in the error constuctor or simply matching a string in the error message like so:

try {
  someFunctionWhichMayThrowSpecificError();
  // a stupid mistake
  let obj;
  if (obj.prop === "something") {
    // do something
  }
} catch (e) {
  if (e.networkError) {
    // code to handle one specific error case
  } else throw e;
}

This plugin provides a simple eslint rule to warn about catch blocks that don't rethrow. Of course there might sometimes be good reasons to catch all, but in those cases a simple // eslint-disable-next-line no-catch-all/no-catch-all will make the intention known better.

Configuration

Install the module via `yarn add -D eslint-plugin-no-catch-all" or "npm install --save-dev eslint-plugin-no-catch-all". Than configure it in your eslintrc file like so:

{
	...,
	"plugins": [
		...,
		"no-catch-all"
	],
	"rules": {
		...,
		"no-catch-all/no-catch-all": "warn"
	}
}

FAQs

Package last updated on 27 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.