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

@carnesen/error-like

v0.2.0

Published

Handle exceptions type-safely

Downloads

60

Readme

build status badge

A utility library for handling TypeScript exceptions type-safely. This package is thoroughly documented, tested, and actively maintained. I use it extensively in my own projects, and you're welcome to use it too!

Usage

To install this package as a dependency in your project, in a shell do:

npm install --save @carnesen/error-like

This package includes runtime JavaScript files and the corresponding TypeScript type declarations.

A common use case for this package is handling exceptions in a type-safe way. For example we can check the error code:

import { errorLikeFromException } from '@carnesen/error-like';

try {
   fs.writeFileSync("foo.dat", "foo-bar-baz");
} catch (exception) {
   const errorLike = errorLikeFromException(exception);
   if (errorLike.code === "ENOENT") {
      // No such file or directory. This is expected if the file doesn't exist.
   } else {
      throw exception;
   }

}

Another use case is logging exceptions:

import { stringifyException } from '@carnesen/error-like';

try {
   something();
} catch (exception) {
   // The `toString` method on an `Error` object includes 
   // the message and the name properties but it does
   // NOT include the call stack. This is no good:
   console.log(`Worst way to log an exception: ${exception}`);
   // If we call `console.log` on the full `Error` object, it's 
   // kind enough to includes the full call stack. This is ok:
   console.log(exception);
   // That's still not great though because from it's not always 
   // easy to look at the logged exception even with the call stack
   // and infer where it originates from in our code. It's best to
   // include in the log message both a unique string that we
   // can search for in our codebase as well as all the salient
   // properties of the original exception. This is best:
   console.log(`Something failed: ${stringifyException(exception)}`)
}

Another use case is JSON serializing Error objects:

import { errorLikeFromException } from '@carnesen/error-like';

const error = new Error('Oops');

console.log(JSON.stringify(error));
// {}

console.log(errorLikeFromException(error));
// {
//   name: 'Error',
//   message: 'Oops',
//   stack: 'Error: Oops\n' +
//     '    at Object.<anonymous> ...'
// }

More information

If you encounter any bugs or have any questions or feature requests, please don't hesitate to file an issue or submit a pull request on this project's repository on GitHub.

License

MIT © Chris Arnesen