@putout/plugin-try-catch
🐊Putout plugin adds ability to apply try-catch
Last updated 4 years ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @putout/plugin-try-catch 
SYNC missed versions from official npm registry.

@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

Rules

Rule

{
    "rules": {
        "try-catch/await": "on",
        "try-catch/args": "on",
        "try-catch/apply-destructuring": "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');

apply-destructuring

Checkout in ????Putout Editor.

❌ Example of incorrect code

import {tryCatch} from 'try-catch';
import {tryToCatch} from 'try-to-catch';

βœ… Example of correct code

import {tryCatch} from 'try-catch';
import {tryToCatch} from 'try-to-catch';

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-args

❌ 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

Current Tags

  • 8.0.0                                ...           latest (a month ago)

13 Versions

  • 8.0.0                                ...           a month ago
  • 7.0.0                                ...           3 months ago
  • 6.1.0                                ...           5 months ago
  • 6.0.0                                ...           a year ago
  • 5.0.0                                ...           a year ago
  • 4.0.0                                ...           2 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.3.0                                ...           4 years ago
  • 1.2.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (0)
None
Dev Dependencies (8)
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |