Common errors
npm install @blackglory/errorssh
npm install --save @blackglory/errors
or
yarn add @blackglory/errors
`API
`ts
type CustomErrorConstructor =
new (message?: string) => Tinterface SerializableError {
name: string
message: string
stack: string | null
ancestors: string[]
}
`$3
`ts
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.$3
`ts
class AssertionError extends CustomError {}
`$3
`ts
function isError(val: unknown): val is Error
function isntError(val: T): val is Exclude
`$3
`ts
function normalize(err: Error): SerializableError
`$3
`ts
function hydrate(err: SerializableError): Error
`$3
`ts
function isSerializableError(val: unknown): val is SerializableError
`$3
`ts
/**
* @throws {AssertionError}
*/
function assert(condition: unknown, message?: string): asserts condition
`$3
`ts
function getErrorNames(err: Error | SerializableError): Iterable
`$3
`ts
function traverseErrorPrototypeChain(err: Error): Iterable
``