npm.io
3.1.0 • Published 5 months ago

@blackglory/errors

Licence
MIT
Version
3.1.0
Deps
2
Size
21 kB
Vulns
0
Weekly
0

errors

Common errors.

Install

npm install --save @blackglory/errors
# or
yarn add @blackglory/errors

API

type CustomErrorConstructor<T extends CustomError = CustomError> =
  new (message?: string) => T

interface SerializableError {
  name: string
  message: string
  stack: string | null
  ancestors: string[]
}
CustomError
class CustomError extends Error {}

CustomError has better default behaviors than Error:

  • console.error prints the correct exception name, not Error.
  • instanceof operator matches based on names rather than inheritance relationships, which helps SerializableError instanceof CustomError.
AssertionError
class AssertionError extends CustomError {}
isError
function isError(val: unknown): val is Error
function isntError<T>(val: T): val is Exclude<T, Error>
normalize
function normalize(err: Error): SerializableError
hydrate
function hydrate(err: SerializableError): Error
isSerializableError
function isSerializableError(val: unknown): val is SerializableError
assert
/**
 * @throws {AssertionError}
 */
function assert(condition: unknown, message?: string): asserts condition
getErrorNames
function getErrorNames(err: Error | SerializableError): Iterable<string>
traverseErrorPrototypeChain
function traverseErrorPrototypeChain(err: Error): Iterable<Error>