JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 217
  • Score
    100M100P100Q78912F
  • License MIT

A solid BaseError class that supports custom properties and wrapping errors

Package Exports

  • baseerr

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (baseerr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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