# @grimen/mybad

> My friendly error base class - for Node/JavaScript.

Latest version **0.3.2** (published 2021-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @grimen/mybad
pnpm add @grimen/mybad
yarn add @grimen/mybad
bun add @grimen/mybad
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2021-04-23 |
| First published | 2019-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 4 |
| Unpacked size | 49.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jonas Grimfelt |
| Maintainers | grimen |
| Keywords | error, errors, baseclass, meta, debug, inspector, pretty, color, colors, detailed, stacktrace |

## Links

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

## Dependencies (4)

- [chalk](https://npm.io/package/chalk.md) ^4.1.0
- [es6-error](https://npm.io/package/es6-error.md) ^4.1.1
- [util-inspect](https://npm.io/package/util-inspect.md) ^0.1.8
- [util-deprecate](https://npm.io/package/util-deprecate.md) ^1.0.2

## 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

- 0.3.2 (latest) — 2021-04-23
- 0.3.1 — 2021-04-23
- 0.3.0 — 2021-04-15
- 0.2.7 — 2021-03-26
- 0.2.6 — 2021-03-26
- 0.2.5 — 2021-03-25
- 0.2.4 — 2021-03-25
- 0.2.3 — 2021-02-03
- 0.2.1 — 2019-08-09
- 0.2.0 — 2019-05-10
- 0.1.1 — 2019-03-31
- 0.1.0 — 2019-03-31

## README

# `js-mybad` [![NPM version](https://badge.fury.io/js/%40grimen%2Fmybad.svg)](https://badge.fury.io/js/%40grimen%2Fmybad) [![Build Status](https://travis-ci.com/grimen/js-mybad.svg?token=sspjPRWbecBSpceU8Jyn&branch=master)](https://travis-ci.com/grimen/js-mybad) [![Coverage Status](https://codecov.io/gh/grimen/js-mybad/branch/master/graph/badge.svg)](https://codecov.io/gh/grimen/js-mybad)

*My friendly error base class - for Node/JavaScript.*


## Introduction

One in general always needs a application/library specific error base class, but the native errors are very limited in what meta/debugging information they can hold at time they are raised. For better debugging and error reporting/inspection this error base class allows to attach some additonal error context information that can be used to better understand the issue - without having to create custom error formatters, or run debugger.


## Install

Install using **npm**:

```bash
$ npm install @grimen/mybad
```

Install using **yarn**:

```bash
$ yarn add @grimen/mybad
```


## Use

Very basic **[example](https://github.com/grimen/js-mybad/tree/master/examples/basic.js)**:

```javascript
const mybad = require('@grimen/mybad')

class ToMuchError extends mybad.Error {}

function printMoney(stash) {
    try {
        if (typeof stash === 'string' && stash.length > 13) {
            throw new RangeError(`Too much money to print: ${stash}`)
        }

        console.log(`PRINT ${stash.join('')}`)

    } catch (error) {
        throw new ToMuchError(error, {
            message: 'Out of money printing ink...',
            id: Date.now(), // well, should maybe be unique...
            key: 'too_much',
            code: 400,
            details: {
                stash,
            },
        })
    }
}

let amount = ''

const range = (to) => {
    return [...Array(to).keys()]
}

for (let dollar of range(42)) {
    amount += '$'

    try {
        printMoney(amount)

    } catch (error) {
        process.env.ERROR_VERBOSE = false
        process.env.ERROR_COLORS = false

        console.log()
        console.log('========================================================')
        console.log('     error.toString({verbose: false})')
        console.log('-----------------------------------------------------')
        console.log()
        console.log(error.toString({verbose: false}))
        console.log()

        console.log()
        console.log('========================================================')
        console.log('     error.toString({verbose: true})')
        console.log('-----------------------------------------------------')
        console.log()
        console.log(error.toString({verbose: true}))
        console.log()

        console.log()
        console.log('========================================================')
        console.log('     error.toJSON()')
        console.log('-----------------------------------------------------')
        console.log()
        console.log(error.toString({verbose: true}))
        console.log()

        throw error
    }
}

```

Run this with optional environment variables `COLORS` / `ERROR_COLORS` and/or `VERBOSE` / `ERROR_VERBOSE` set too truthy or falsy values, so see various error info formatting in terminal. Also `(NO_COLOR)[https://no-color.org]` is respected.

Something like this (imagine some colorized formatting):

```bash
PRINT $
PRINT $$
PRINT $$$
PRINT $$$$
PRINT $$$$$
PRINT $$$$$$
PRINT $$$$$$$
PRINT $$$$$$$$
PRINT $$$$$$$$$
PRINT $$$$$$$$$$
PRINT $$$$$$$$$$$
PRINT $$$$$$$$$$$$
PRINT $$$$$$$$$$$$$


========================================================
     error.toString({verbose: false})
-----------------------------------------------------

Out of money printing ink...


========================================================
     error.toString({verbose: true})
-----------------------------------------------------

Out of money printing ink... { stash: '$$$$$$$$$$$$$$' }


========================================================
     error.toJSON()
-----------------------------------------------------

{
  type: 'ToMuchError',
  id: 1618487081556,
  code: 400,
  key: 'too_much',
  message: 'Out of money printing ink...',
  details: { stash: '$$$$$$$$$$$$$$' },
  stack: [
    {
      file: '/Users/jonas/Dev/Private/js-mybad/examples/basic.js',
      function: 'printMoney',
      line: 23,
      column: 19,
      source: undefined
    },
    {
      file: '/Users/jonas/Dev/Private/js-mybad/examples/basic.js',
      function: 'Object.<anonymous>',
      line: 57,
      column: 9,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Module._compile',
      line: 1063,
      column: 30,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Object.Module._extensions..js',
      line: 1092,
      column: 10,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Module.load',
      line: 928,
      column: 32,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Function.Module._load',
      line: 769,
      column: 14,
      source: undefined
    },
    {
      file: 'internal/modules/run_main.js',
      function: 'Function.executeUserEntryPoint [as runMain]',
      line: 72,
      column: 12,
      source: undefined
    },
    {
      file: '17',
      function: 'internal/main/run_main_module.js',
      line: 47,
      column: undefined,
      source: undefined
    }
  ]
}
```


## Test

Clone down source code:

```sh
$ make install
```

Run **colorful tests** using **jest**:

```sh
$ make test
```


## Related

- [**`python-mybad`**](https://github.com/grimen/python-mybad) - *"My friendly error base class - for Python"*


## About

This project was mainly initiated - in lack of solid existing alternatives - to be used at our work at **[Markable.ai](https://markable.ai)** to have common code conventions between various programming environments where **Node.js** (for I/O heavy operations) is heavily used.


## License

Released under the MIT license.

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