@pacote/error
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

@pacote/error

version minified minified + gzip

Custom error classes and utilities.

Installation

yarn add @pacote/error

Usage

BaseError

BaseError is an error class which provides a convenience static imprint() method to get around issues with extending the built-in JavaScript Error class. It's by no means a bullet-proof solution and full support is not available in older browsers (such as Internet Explorer up to version 10).

import { BaseError } from '@pacote/error'

class StatusError extends BaseError {
  constructor(public readonly status: number, message?: string) {
    super(message)
    StatusError.imprint(this)
  }
}

const e = new StatusError(404, 'Not found')

e instanceOf Error       // true
e instanceOf BaseError   // true
e instanceOf StatusError // true
e.message                // 'Not found'
e.status                 // 404
e.stack                  // <defined>

ComplexError

ComplexError aggregates multiple error objects so they can be thrown as one. Its primary use case is to support validation errors caused by multiple failing conditions.

import { ComplexError } from '@pacote/error'

function doSomething() {
  throw new ComplexError('Could not do something', [
    new Error('one'),
    new Error('two'),
  ])
}

try {
  doSomething()
} catch (e) {
  e.causes.forEach(console.error) // Outputs Error('one'), Error('two')
}

License

MIT © Luís Rodrigues.

/@pacote/error/

    Package Sidebar

    Install

    npm i @pacote/error

    Weekly Downloads

    17

    Version

    3.0.2

    License

    MIT

    Unpacked Size

    17 kB

    Total Files

    13

    Last publish

    Collaborators

    • goblindegook