Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@putout/plugin-try-catch

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@putout/plugin-try-catch

🐊Putout plugin adds ability to apply try-catch

  • 4.0.0
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@putout/plugin-try-catch NPM version

The try...catch statement marks a try block and a catch block. If the code in the try block throws an exception then the code in the catch block will be executed.

(c) MDN

🐊Putout plugin adds support of:

Which are drastically simplifies try...catch blocks.

Install

npm i @putout/plugin-try-catch

Rule

{
    "rules": {
        "try-catch/await": "on",
        "try-catch/args": "on",
        "try-catch/declare": "on",
        "try-catch/expand-arguments": "on",
        "try-catch/sync": "on",
        "try-catch/async": "on"
    }
}

sync

❌ Example of incorrect code

try {
    log('hello');
} catch(error) {}

✅ Example of correct code

import tryCatch from 'try-catch';

const [error] = tryCatch(log, 'hello');

async

❌ Example of incorrect code

try {
    await send('hello');
} catch(error) {}

✅ Example of correct code

import tryToCatch from 'try-catch';

const [error] = await tryToCatch(send, 'hello');

await

❌ Example of incorrect code

await tryCatch(a, b);
tryToCatch(a, b);

✅ Example of correct code

await tryToCatch(a, b);

args

❌ Example of incorrect code

tryCatch(send('hello'));

✅ Example of correct code

tryCatch(send, 'hello');

declare

❌ Example of incorrect code

const [error] = tryCatch(fs.readFileSync, 'hello.txt');

✅ Example of correct code

import tryCatch from 'try-catch';

const [error] = tryCatch(fs.readFileSync, 'hello.txt');

expand-arguments

❌ Example of incorrect code

import tryCatch from 'try-catch';

test('some message', (t) => {
    const fn = () => copymitter('/hello');
    const [error] = tryCatch(fn);
    
    t.equal(error.message, 'to should be a string!');
    t.end();
});

✅ Example of correct code

import tryCatch from 'try-catch';

test('some message', (t) => {
    const [error] = tryCatch(copymitter, '/hello');
    
    t.equal(error.message, 'to should be a string!');
    t.end();
});

License

MIT

Keywords

FAQs

Package last updated on 21 Aug 2024

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc