# abstract-error

> abstract error class with error code supports to create error class quickly

Latest version **2.0.1** (published 2026-07-10) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2026-07-10 |
| First published | 2015-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 39.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Riceball LEE |
| Maintainers | riceball |
| Keywords | buffer, codec, encode, decode, serialization, serialize, deserialize, bytewise, lexicographic, sort, index, object, class |

## Links

- npm: https://www.npmjs.com/package/abstract-error
- Repository: https://github.com/snowyu/abstract-error.js
- Issues: https://github.com/snowyu/abstract-error.js/issues
- npm.io page: https://npm.io/package/abstract-error

## Dependencies (1)

- [inherits-ex](https://npm.io/package/inherits-ex.md) ^2.2.2

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2026-07-10
- 2.0.0-alpha.8 (alpha) — 2024-04-02
- 2.0.0 — 2024-08-29
- 2.0.0-alpha.7 — 2024-03-28
- 2.0.0-alpha.6 — 2024-03-28
- 2.0.0-alpha.5 — 2024-03-28
- 2.0.0-alpha.4 — 2023-08-15
- 2.0.0-alpha.3 — 2023-08-15
- 2.0.0-alpha.2 — 2023-08-15
- 2.0.0-alpha.1 — 2023-08-15
- 2.0.0-alpha.0 — 2023-04-16
- 1.0.2 — 2015-07-26
- 1.0.1 — 2015-02-03
- 1.0.0 — 2015-02-03

## README

### AbtractError [![Build Status](https://img.shields.io/travis/snowyu/abstract-error.js/master.png)](http://travis-ci.org/snowyu/abstract-error.js) [![npm](https://img.shields.io/npm/v/abstract-error.svg)](https://npmjs.org/package/abstract-error) [![downloads](https://img.shields.io/npm/dm/abstract-error.svg)](https://npmjs.org/package/abstract-error) [![license](https://img.shields.io/npm/l/abstract-error.svg)](https://npmjs.org/package/abstract-error)

abstract error class with error code supports to create error class quickly.

# Classes

# AbstractError

All Errors are derived from the AbstractError.

```javascript
import { AbstractError, createErrorClass } from 'abstract-error'
```

* Members:
  * `message`: the error message.
  * `code`: the error code.
* Class Methods:
  * `createErrorClass(aType: string, aErrorCode: number, ParentErrorClass=AbstractError): typeof AbstractError`

## CommonError

```javascript
import { CommonError } from 'abstract-error'
```

`CommonError` derived from the `AbstractError`. All Common Errors are derived from the `CommonError`.
The `CommonError` use the number as error code.

* Members:
  * message: the error message.
  * code: the error code.
* Methods: return true if the error instance is this error type.
  * ok()
  * notFound()
  * ....
  * invalidFormat()
* Class Methods:
  * `createErrorClass(aType: string, aErrorCode?: number, ParentErrorClass=CommonError): typeof CommonError`
  * CommonError.isOk(err)
  * CommonError.isNotFound(err)
  * ...

the error codes:

* CommonError.Ok              = 0
* CommonError.NotFound        = 1
* CommonError.Corruption      = 2
* CommonError.NotSupported    = 3
* CommonError.InvalidArgument = 4
* CommonError.IO              = 5
* CommonError.NotOpened       = 6
* CommonError.InvalidType     = 7
* CommonError.InvalidFormat   = 8


## Other Error Classes

* Errors.NotFoundError
* Errors.CorruptionError
* Errors.NotSupportedError/NotImplementedError
* Errors.InvalidArgumentError
* Errors.IOError
* Errors.NotOpenedError
* Errors.InvalidTypeError
* Errors.InvalidFormatError


## Extends the AbstractError

use the `createErrorClass` function can extend the AbstractError.

`createErrorClass(typeName, errorCode?: number|string, parentErrorClass?: typeof AbstractError): typeof AbstractError`

__arguments__

* `typeName` *(string)*: the error type name, the first character must be upper case.
* `errorCode`: *(number|string)*: the optional error code, it should be not equal 0 if it's a number.
* `parentErrorClass`: *(class)*:  the optional parent error class. defaults to `AbstractError`.

__return__

* the new error class


### Usage

```js
import {CommonError, Errors, createCommonErrorClass} from 'abstract-error';

const NotFoundError = Errors.NotFoundError
const AlreadyReadError = createCommonErrorClass('AlreadyRead', 10000)

const err = new AlreadyReadError('already read over error.')

assert.ok(CommonError.isAlreadyRead(err))
assert.ok(AlreadyReadError.isAlreadyRead(err))
assert.ok(err.alreadyRead())
assert.equal(err.message, 'already read over error.')
assert.equal(err.code, 10000)

```

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