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

enhanced-exception

v1.0.2

Published

Special "Exception" class to significantly improve your stack traces.

Downloads

106

Readme

Have you come recently from Java or C# world into JavaScript and you start wondering how the hell we are living here with the basic Error constructor? Yea, our stack traces aren't well descriptive, BUT here it is - Exception class just for you! Dive deep into the Quick showcase and see how this little lib can significantly improve your stack traces today.

Tip of the day - If you are coding in TypeScript, remember to output source maps and also enable them in Node.

Table of contents

Quick showcase

For those who don't like to read a lot.

Code

async function accessFileSystemFile() {
  // some neat code here...
  throw new Error('I/O Error');
}

async function readLatestLog() {
  try {
    // some neat code here...
    return await accessFileSystemFile();
  } catch(e: unknown) {
    throw new Exception('Unable to read log file.', e);
  }
}

async function bootstrap() {
  try {
    await readLatestLog();
  } catch (e: unknown) {
    throw new Exception('Failed to do some dangerous stuff.', e);
  }
}

void bootstrap();

Stack trace

Exception: Failed to do some dangerous stuff.
    at file:///path/to/project/src/real-world-example.js:26:19
    at Generator.throw (<anonymous>)
    at rejected (/path/to/project/node_modules/tslib/tslib.js:113:69)
Caused by: Exception: Unable to read log file.
    at file:///path/to/project/src/real-world-example.js:16:19
    at Generator.throw (<anonymous>)
    at rejected (/path/to/project/node_modules/tslib/tslib.js:113:69)
Caused by: Error: I/O Error
    at file:///path/to/project/src/real-world-example.js:6:15
    at Generator.next (<anonymous>)
    at /path/to/project/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)
    at __awaiter (/path/to/project/node_modules/tslib/tslib.js:111:16)
    at accessFileSystemFile (file:///path/to/project/src/real-world-example.js:4:12)
    at file:///path/to/project/src/real-world-example.js:13:26
    at Generator.next (<anonymous>)
    at /path/to/project/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)

Installation

npm install --save enhanced-exception

or

yarn add enhanced-exception

API

Exception class

| element | signature | comment | |-------------|----------------------------------------|--------------------------------------------------------------| | constructor | (message?: string, previous?: unknown) | Creates new Exception class object. | | .message | string | Exception message, the same as in classic Error object. | | .name | string | Exception class name, the same as in classic Error object. | | .previous | unknown | Previous error, provided via constructor. |

Custom exceptions

Thanks to the fact that PlayerNotFoundException class extends Exception class, it also inherits all it's capabilities.

class PlayerNotFoundException extends Exception {
  playerName: string;

  constructor(playerName: string, previous?: unknown) {
    super(`Player "${playerName}" not found.`, previous);
    this.playerName = playerName;
  }
}
try {
  // some neat code here...
  throw new Error('Database record not found');
} catch(e: unknown) {
  throw new PlayerNotFoundException('IdkMan2', e);
}
PlayerNotFoundException: Player "IdkMan2." not found.
    at file:///path/to/project/src/real-world-example.js:13:11
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Caused by: Error: Database record not found
    at file:///path/to/project/src/real-world-example.js:10:11
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  playerName: 'Failed to do some dangerous stuff.'
}