Package detail

baseerr

tjmehta1.7kMIT2.0.6

A solid BaseError class that supports custom properties and wrapping errors

base error, custom error, base error class, custom error class

readme

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 // 'CustomError'
console.log(err.stack)
// CustomError: boom
//     anonymous (filename:5:17)

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)
  // CustomError: caught error
  //     anonymous (filename:8:17)
  // ----
  // Error: pow
  //     anonymous (filename:6:9)
}

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),
)
// rejects with:
// CustomError: caught error
//     anonymous (filename:6:3)
// ----
// Error: pow
//     anonymous (filename:5:16)

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) // 10
console.log(err.bar) // 20
console.log(err.stack)
// CustomError: boom
//     anonymous (filename:5:17)
// {
//   "foo": 10,
//   "bar": 20
// }

// TypeScripters use BaseError.create if you want to access extended properties with proper typing:
const err = CustomError.create('boom', { foo: 10, bar: 20 })
console.log(err.foo) // 10
console.log(err.bar) // 20

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 })
      // ApiError: boom
      //     anonymous (filename:row:col)
      // {
      //   "url": 'https://localhost:3000',
      //   "statusCode": 504
      // }
      // ----
      // TimedoutError: request timed out
      //     anonymous (filename:row:col)
      // {
      //   "url": 'https://localhost:3000',
      //   "statusCode": 504
      // }
    }
  }
}

Checkout the tests for more examples..

License

MIT

changelog

v2.0.6

  • patch: publish err

v2.0.5

  • patch: fix exports.types in pkg

v2.0.4

  • patch: fix tests

v2.0.3

  • patch: fix build

v2.0.2

  • patch: improved build script

v2.0.1

  • patch: package.json types

v2.0.0

  • major: fix cjs and esm support
  • patch: ts fixes

v1.0.1

  • patch: improved support for regexp stringification in stacks

v1.0.0

  • major initial implementation with tests