# throwable

> Inherit from Error without performance penalties, instanceof behaves as expected and the error stack is correct

Latest version **1.0.3** (published 2015-03-06) · MIT license · 0 weekly downloads

## Install

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

## 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.3 |
| Published | 2015-03-06 |
| First published | 2015-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Joris van der Wel |
| Maintainers | joris-van-der-wel |
| Keywords | error, exception, throwable, inherit, inherits, stack, performance, browser, browserify, catch, instanceof, wrap, decorator |

## Links

- npm: https://www.npmjs.com/package/throwable
- Repository: https://github.com/Joris-van-der-Wel/node-throwable
- Issues: https://github.com/Joris-van-der-Wel/node-throwable/issues
- npm.io page: https://npm.io/package/throwable

## 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.3 (latest) — 2015-03-06
- 1.0.2 — 2015-03-06
- 1.0.1 — 2015-03-06
- 1.0.0 — 2015-03-04

## README

node-throwable
==============
Inherit from Error without performance penalties, `instanceof` behaves as expected and the error stack is correct. This library can be used cross-browser using Browserify.

Usage
-----
A lot of solutions for inheriting from `Error` in javascript are error-prone (pun intended) and tricky.

Using this library you can inherit `Error` just like you would inherit from any other javascript object/class.
Here is an example using the [inherits](https://www.npmjs.com/package/inherits) library.

```javascript
var Throwable = require('throwable');

function MyError(wrapped)
{
        Throwable.call(this, wrapped);
        this.name = 'MyError';
}

require('inherits')(MyError, Throwable);
```

And this is how you can throw your error:
```javascript
function failure()
{
        throw new MyError(Error('Something went wrong!'));
}
```

Advantages
----------
This library has a few advantage some other solutions might not have:

Using `instanceof` wil work as expected:

```javascript
try
{
        failure();
}
catch(err)
{
        console.log(err instanceof Error); // true
        console.log(err instanceof Throwable); // true
        console.log(err instanceof MyError); // true
}
```

And the `stack`, `fileName` (firefox), `lineNumber` (firefox) attributes will be set correctly (instead of pointing to a line in the Throwable constructor).

Also, the `stack` attribute is accessed only when you need it, `Error.captureStackTrace` is not used either. Accessing any of these two is a big performance hit in Node.js and Chrome.

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