abstract-error

2.0.0-alpha.0 • Public • Published

AbtractError Build Status npm downloads license

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

AbstractError Classes

AbstractError

All Errors are derived from the AbstractError.

  • 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:
    • AbstractError.isOk(err)
    • AbstractError.isNotFound(err)
    • ...

the error codes:

  • AbstractError.Ok = 0
  • AbstractError.NotFound = 1
  • AbstractError.Corruption = 2
  • AbstractError.NotSupported = 3
  • AbstractError.InvalidArgument = 4
  • AbstractError.IO = 5
  • AbstractError.NotOpened = 6
  • AbstractError.InvalidType = 7
  • AbstractError.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 createError function can extend the AbstractError.

createError(typeName, errorCode[, parentErrorClass])

arguments

  • typeName (string): the error type name, the first character must be upper case.
  • errorCode: (number): the error code, it should be greater than 1000.
  • parentErrorClass: (class): the optional parent error class. defaults to AbstractError.

return

  • the error class

Usage

import {AbstractError, Errors, createError} from 'abstract-error';

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

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

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

Package Sidebar

Install

npm i abstract-error

Weekly Downloads

13

Version

2.0.0-alpha.0

License

MIT

Unpacked Size

20.6 kB

Total Files

10

Last publish

Collaborators

  • riceball