# lefu

> Handling named try catch errors

Latest version **1.0.0** (published 2019-07-11) · 0 weekly downloads

## Install

```sh
npm install lefu
pnpm add lefu
yarn add lefu
bun add lefu
```

## 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.0 |
| Published | 2019-07-11 |
| First published | 2019-07-11 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Paolo Roth |
| Maintainers | octod |
| Keywords | errors, error handling, typescript |

## Links

- npm: https://www.npmjs.com/package/lefu
- Repository: https://github.com/yourname/yourrepo
- Homepage: https://github.com/yourname/yourrepo#readme
- Issues: https://github.com/yourname/yourrepo/issues
- npm.io page: https://npm.io/package/lefu

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-07-11

## README

lefu [![Build Status](https://travis-ci.com/OctoD/typed-try-catch.svg?token=tdmJoshCkwxwDH17QdBY&branch=master)](https://travis-ci.com/OctoD/typed-try-catch)
====

Shit happens. 

Always.

In every single line of code.

Handling errors is an hassle in JavaScript, because you do not have multiple
catch block for typed errors.

This is a function way to handle try/catch constructs functionally.

# Install

```bash
# npm
npm i lefu

# yarn
yarn add lefu
```

# Usage

You can use this library as a full try/catch construct

```ts
import { Try, Catch } from 'lefu';

const result = Try(
    () => 1 + a,
    Catch.Error(() => 'OMG, an error'),
    Catch.TypeError(() => 'Darn! A type error'),
    Catch.CustomError(() => '🦄'),
    // ... all other errors
  );

console.log(result) // 'Darn! A type error'
```

you can also try/catch async functions

```ts
import { TryAsync, Catch } from 'lefu';

const result = Try(
    async () => {
      throw new TypeError('💥💥🐛💥💥');
    },
    Catch.Error(() => 'OMG, an error'),
    Catch.TypeError(() => 'Darn! A type error'),
    Catch.CustomError(() => '🦄'),
    // ... all other errors
  );

console.log(result) // Promise('Darn! A type error')
```

You can use this library for executing a function when an error is caught

```ts
import { handlers } from 'lefu';

try {
  1 + a
} catch (error) {
  handlers.Error(error)(() => console.log('it is an Error'));
  handlers.TypeError(error)(() => console.log('it is a TypeError'));
  handlers.MyCustomError(error)(() => console.log('it is a user defined error'));
}

// output:
//  it is a TypeError
```

Or you can use it to check if an error is of a certain type


```ts
import { match } from 'lefu';

try {
  1 + a
} catch (error) {
  console.log(match(error).Error); // false
  console.log(match(error).TypeError); // true
  console.log(match(error).MyCustomError); // false
}

try {
  throw { name: 'MyCustomError', message: 'I broke it' };
} catch (error) {
  console.log(match(error).Error); // false
  console.log(match(error).TypeError); // false
  console.log(match(error).MyCustomError); // true
}

// output:
//  it is a TypeError
```

# Contributing

Read the [contributing guidelines](./CONTRIBUTING.md)

# Licence

Read the [licence](./LICENCE)

💩💩💩

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