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

@rogwilco/try

v2.1.0

Published

A lightweight module that enables scoped catch blocks.

Downloads

10

Readme

Try

NPM Package Build Status Code Coverage

A lightweight module that enables typed catch blocks.

Requirements

Note: Please consider these caveats for ES5 and TypeScript 2.1 when using this library in an unsupported environment.

Install

$ npm install @rogwilco/try

Documentation

TypeDoc Documentation

Usage

Try can be used in two different styles depending on preference.

Chaining

The chaining API completely replaces the usage of native try/catch/finally blocks. Simply start a chain with Try(), providing a function for the try block, followed by similar invocations of Catch() for each type of error you want to handle, and an optinal Finally() block. All usages must include a trailing call to Done() to ensure your code is executed.

import Try from 'Try'

Try(() => {
  // This block will be executed until an error is thrown.
  throw new ReferenceError('Something went wrong!')
})
  .Catch(ReferenceError, e => {
    // This block is only invoked if a ReferenceError is thrown.
    console.error(`Reference Error: ${e.message}`)
  })
  .Catch(TypeError, e => {
    // This block is only invoked if a TypeError is thrown.
    console.error(`Type Error: ${e.message}`)
  })
  .Finally(() => {
    // This block is executed regardless of wheter any errors were caught.
  })
  .Done()

Native

The native approach preserves the use of native try/catch/finally blocks, and provides a Handle() function for use within the catch block. The Handle() function will manage the handling of different error types (and will automatically re-throw an unmatched error).

import { Handle } from 'Try'

try {
  // This block will be executed until an error is thrown.
  throw new ReferenceError('Something went wrong!')
} catch (e) {
  Handle(e, {
    // This block is only invoked if a ReferenceError is thrown.
    ReferenceError: e => {
      console.error(`Reference Error: ${e.message}`)
    },
    // This block is only invoked if a TypeError is thrown.
    TypeError: e => {
      console.error(`Type Error: ${e.message}`)
    },
  })
} finally {
  // This block is executed regardless of wheter any errors were caught.
}