@putout/plugin-try-catch
Version:
πPutout plugin adds ability to apply try-catch
160 lines (109 loc) β’ 2.79 kB
Markdown
//img.shields.io/npm/v/@putout/plugin-try-catch.svg?style=flat&longCache=true
[ ]: https://npmjs.org/package/@putout/plugin-try-catch "npm"
> 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**](https://github.com/coderaiser/putout) plugin adds support of:
- β
[try-catch](https://github.com/coderaiser/try-catch)
- β
[try-to-catch](https://github.com/coderaiser/try-to-catch)
Which are drastically simplifies [`try...catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) blocks.
```
npm i @putout/plugin-try-catch
```
- β
[await](
- β
[args](
- β
[declare](
- β
[expand-arguments](
- β
[sync](
- β
[async](
```json
{
"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"
}
}
```
```js
try {
log('hello');
} catch(error) {}
```
```js
import tryCatch from 'try-catch';
const [error] = tryCatch(log, 'hello');
```
```js
try {
await send('hello');
} catch(error) {}
```
```js
import tryToCatch from 'try-catch';
const [error] = await tryToCatch(send, 'hello');
```
```js
await tryCatch(a, b);
tryToCatch(a, b);
```
```js
await tryToCatch(a, b);
```
```js
tryCatch(send('hello'));
```
```js
tryCatch(send, 'hello');
```
```js
const [error] = tryCatch(fs.readFileSync, 'hello.txt');
```
```js
import tryCatch from 'try-catch';
const [error] = tryCatch(fs.readFileSync, 'hello.txt');
```
```js
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();
});
```
```js
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();
});
```
MIT
[ ]: https: