npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

baseerr

v2.0.6

Published

A solid BaseError class that supports custom properties and wrapping errors

Downloads

668

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