3.0.2 • Published 9 months ago

@pacote/error v3.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@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.

3.0.2

9 months ago

3.0.1

1 year ago

3.0.0

1 year ago

2.1.7

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.0.0

6 years ago