3.0.0 β’ Published 10 months ago
@putout/plugin-try-catch v3.0.0
@putout/plugin-try-catch
The
try...catch
statement marks atry
block and acatch
block. If the code in thetry
block throws an exception then the code in thecatch
block will be executed.(c) MDN
πPutout plugin adds support of:
- β try-catch
- β try-to-catch
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