Giter Club home page Giter Club logo

eslint-plugin-no-catch-all's Introduction

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"
	}
}

eslint-plugin-no-catch-all's People

Contributors

mrloh avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

eslint-plugin-no-catch-all's Issues

Thank you!

Just wanted to show my appreciation. This was a common source of bugs for me and I was writing unit tests to ensure that errors are bubbled. Surprised this plugin isn't more popular, it's crucial for me!

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.