# maf-error

> checkable errors

Latest version **1.2.0** (published 2017-08-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install maf-error
pnpm add maf-error
yarn add maf-error
bun add maf-error
```

## 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.2.0 |
| Published | 2017-08-23 |
| First published | 2017-02-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | alekzonder |
| Maintainers | alekzonder |
| Keywords | maf |

## Links

- npm: https://www.npmjs.com/package/maf-error
- Repository: https://github.com/mafjs/error
- Homepage: https://github.com/mafjs/error#readme
- Issues: https://github.com/mafjs/error/issues
- npm.io page: https://npm.io/package/maf-error

## Dependencies (1)

- [terror](https://npm.io/package/terror.md) 1.4.x

## Recent versions

- 1.2.0 (latest) — 2017-08-23
- 1.1.0 — 2017-08-18
- 1.0.1 — 2017-07-14
- 1.0.0 — 2017-07-07
- 0.9.3 — 2017-03-09
- 0.9.2 — 2017-03-01
- 0.9.1 — 2017-03-01
- 0.9.0 — 2017-03-01
- 0.1.0 — 2017-02-27

## README

# maf-error

checkable errors

[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)

[![bitHound Overall Score](https://www.bithound.io/github/mafjs/error/badges/score.svg)](https://www.bithound.io/github/mafjs/error)
[![bitHound Dependencies](https://www.bithound.io/github/mafjs/error/badges/dependencies.svg)](https://www.bithound.io/github/mafjs/error/master/dependencies/npm)
[![Build Status](https://travis-ci.org/mafjs/error.svg?branch=master)](https://travis-ci.org/mafjs/error)
[![Coverage Status](https://coveralls.io/repos/github/mafjs/error/badge.svg?branch=master)](https://coveralls.io/github/mafjs/error?branch=master)

[![NPM](https://nodei.co/npm/maf-error.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/maf-error/)



based on [terror](https://github.com/nodules/terror) package

# install

```js
npm i maf-error
```

# why?

errors usually checks with `if`

```js
// } catch (error) {
    if (error instanceof TaskError) {

        if (error.code === 'some_error_code') {
            // action ...
        } else if (/* ... */) {
            // ...
        } else {
            // ...
        }

    } else {
        // ...
    }

// }
```

[terror](https://github.com/nodules/terror) package allows check error type and code, but `if` is still here

```js
// var TaskError = terror.create('TaskError', {SOME_CODE: 'some error code'});
// } catch (error) {

    error = TaskError.ensureError(error);

    if (TaskError.is(TaskError.CODES.SOME_CODE)) {
        // action ...
    } } else if (/* ... */) {
        // ...
    } else {
        // ...
    }

// }
```

With promises you can write promise chain using different classes, methods, apis with different error types.

And got all error handling in one catch function.

```js
// some method: get all tasks in list

api.lists.get(listId)
    .then((list) => {
        return api.tasks.find({listId: list.id});
    })
    .then((tasks) => {
        res.json(tasks);
    })
    .catch((error) => {
        // here can get ListError, TaskError from apis
        // and other errors

        // and if checks here

        if (error instanceof ListError) {
            if (ListError.is(ListError.CODES.NOT_FOUND, error)) {
                res.status(404).json('not found');
            } else if (ListError.is(/* ... */)) {
                // ...
            } else {
                // ...
            }
        } else if (error instanceof TaskError) {
            // many if here
        } else {
            logger.error(error);
            res.status(500).json('server error');
        }

    });
```

too much `if`, too much code

using `maf-error`

```js
require('maf-error/initGlobal');

// same method and  same promise chain
//
    .catch((error) => {

        Error.ensureCheckChain(error)
            .if(ListError, {
                [ListError.CODES.NOT_FOUND]: (error) => {
                    res.status(404).json('not found');
                },

                [ListError.CODES.FORBIDDEN]: (error) => {
                    // ...
                }
            })
            .if(TaskError, {
                // ...
            })
            .else((error) => {
                logger.error(error);
                res.status(500).json('server error');
            })
            .check();

    });

```

clean and simple imho

# usage

see [examples](https://github.com/mafjs/error/tree/master/examples)

# LICENSE

MIT

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