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

checked-exceptions

v1.2.0

Published

A utility library to create and manage checked exceptions in typescript

Downloads

4,821

Readme

checked-exceptions

Build Status npm

A utility library to create and manage checked exceptions in typescript.

Index

Installation

npm:

npm i checked-exceptions --save

yarn:

yarn add checked-exceptions

Usage

Create

A checked exception can be created by just importing check function for the library and passing it the type of the exception.

import {check} from 'checked-exceptions'

const NotImplemented = check('NotImplemented')

throw new NotImplemented()

Instantiate

Once a checked exception class has been created, an instance of the checked exception can be created using the new operator or using the static of() function, for eg:

Using the new operator

const err = new NotImplemented()

Using the of function

const err = NotImplemented.of()

Customize

A custom exception with additional meta data can be created by passing a second argument for eg:

type User = {id: number; name: string}

const UserIdNotFound = check(
  'UserIdNotFound',
  (user: User) => `Could not find user with id: ${user.id}`
)

throw new UserIdNotFound({id: 1900, name: 'Foo'})

The second argument is of type function and is used to generate the message string when the exception is thrown.

Access Data

The default properties of an exception such as stack and message work like they do in a typical Error object. Additional properties such as data , type is also added, for eg:

type User = {id: number; name: string}

const UserIdNotFound = check(
  'UserIdNotFound',
  (user: User) => `Could not find user with id: ${user.id}`
)

const err = new UserIdNotFound({id: 1900, name: 'Foo'})

console.log(err.data.id) // prints 1900
console.log(err.type) // prints 'UserIdNotFound'

Access Type

To access the type of the exception you can use the info property on the created checked exception —

type User = {id: number; name: string}

const UserIdNotFound = check(
  'UserIdNotFound',
  (user: User) => `Could not find user with id: ${user.id}`
)

type UserIdNodeFoundException = typeof UserIdNotFound.info

const err: UserIdNodeFoundException = new UserIdNotFound({
  id: 1900,
  name: 'Foo'
})