BaseErr
A solid BaseError class that supports custom properties and wrapping errors
Installation
npm i --save baseerr
Usage
Create a custom error class
import BaseError from 'baseerr'
class CustomError extends BaseError {}
const err = new CustomError('boom')
console.log(err.name
console.log(err.stack)
Wrap an error with a custom error class
import BaseError from 'baseerr'
class CustomError extends BaseError {}
try {
throw new Error('pow')
} catch (_err) {
const err = CustomError.wrap('caught error', _err)
console.log(err.stack)
}
Wrap an error with a custom error class in promise chain
import BaseError from 'baseerr'
class CustomError extends BaseError {}
Promise.reject(new Error('pow')).catch((err) =>
CustomError.wrapAndThrow('caught error', err),
)
Create custom error instance with data properties
import BaseError from 'baseerr'
class CustomError extends BaseError {}
const err = new CustomError('boom', { foo: 10, bar: 20 })
console.log(err.foo)
console.log(err.bar)
console.log(err.stack)
const err = CustomError.create('boom', { foo: 10, bar: 20 })
console.log(err.foo)
console.log(err.bar)
Create custom api client with robust error handling
import BaseError from 'baseerr'
class FetchError extends BaseError {}
class ResponseError extends BaseError {}
class ApiError extends BaseError {}
class ApiClient {
getData() {
const url = 'https://localhost:3000'
try {
const res = await Promise.race([
timeout(2000).then(() => {
throw new TimeoutError('request timed out', { statusCode: 504, url })
}),
fetch(url).catch(
FetchError.wrapAndThrow('network error', { statusCode: 503, url }),
),
])
if (res.statusCode !== 200) {
throw new ResponseError('status: ${res.statusCode}', {
statusCode: res.statusCode,
url,
})
}
return await res.json()
} catch (err) {
throw ApiError.wrap(err, { url, statusCode: err.statusCode || 500 })
}
}
}
Checkout the tests for more examples..
License
MIT