# @clusic/catch

> The `try catch` block of clusic

Latest version **1.0.4** (published 2018-11-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @clusic/catch
pnpm add @clusic/catch
yarn add @clusic/catch
bun add @clusic/catch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2018-11-07 |
| First published | 2018-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | evio |
| Keywords | catch, try |

## Links

- npm: https://www.npmjs.com/package/@clusic/catch
- Repository: https://github.com/clusic/catch
- Homepage: https://github.com/clusic/catch#readme
- Issues: https://github.com/clusic/catch/issues
- npm.io page: https://npm.io/package/@clusic/catch

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.4 (latest) — 2018-11-07
- 1.0.3 — 2018-10-24
- 1.0.2 — 2018-10-24
- 1.0.1 — 2018-10-23
- 1.0.0 — 2018-10-23

## README

# @clusic / Catch module

主要用来解决回滚问题。

## Install

```shell
npm i @clusic/catch
```

## Usage

```javascript
const Catcher = require('@clusic/catch');

await Catcher(roll => {
  fs.writeFileSync('/test', 'aaaaaa', 'utf8');
  roll(() => fs.unlinkSync('/test')); // 回滚行为
  throw new Error('this is error');
}, error);

function error(e) {
  console.log('catch error', e);
}
```

## Arguments

- callback {function} 具体行为代码
- error {function} 错误处理代码

> 如果没有定义error，则将`throw error`。callback函数自带一个回滚函数即`roll`。

## In Clusic

### onResponseError

请求级别接管错误输出

```javascript
// app.bootstrap.js
module.exports = app => {
  app.use(async (ctx, next) => {
    ctx.onResponseError(error => {
      ctx.status = error.status || 500;
      ctx.body = {
        error: error.messasge
      }
    })
  })
}
```

### onErrorCatch

绑定回滚物理事件

```javascript
fs.writeFileSync('/test.txt', 'hello world', 'utf8');
ctx.onErrorCatch(() => fs.unlinkSync('/test.txt'));
```

### error

生成一个带status的错误信息

```javascript
throw ctx.error('error', 503);
// 相当于
const err = new Error('error');
err.status = 503;
throw err;
```

### sendResult

发送成功结果

```javascript
ctx.sendResult({
  a: 1
}, 126000);
// 相当于
ctx.body = { status: 126000, data: { a: 1 } };
ctx.status = 200;
```

---
_Source: https://npm.io/package/@clusic/catch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
