3.0.3 • Published 9 months ago
@pacote/error v3.0.3
@pacote/error
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.3
9 months ago
3.0.2
2 years ago
3.0.1
2 years ago
3.0.0
2 years ago
2.1.7
4 years ago
2.1.6
5 years ago
2.1.5
5 years ago
2.1.4
5 years ago
2.1.3
5 years ago
2.1.2
5 years ago
2.1.1
6 years ago
2.1.0
6 years ago
2.0.5
6 years ago
2.0.4
6 years ago
2.0.3
6 years ago
2.0.2
7 years ago
2.0.1
7 years ago
2.0.0
7 years ago
1.0.0
7 years ago