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

try-catch-overloaded

v0.1.0

Published

This package provides function wrapper for overloading Try/Catch statements.

Downloads

2

Readme

try-catch-overloaded

This package contains function for overloading Try/Catch statements.

Motivation

Adding custom errors requires manually checking which type of error
we received in a catch block, because JS does not have catch overload. e.g.

class UserNotFoundError extends Error {
    // ... Some implementation;
}

try {
    // ... Some code that can throw UserNotFoundError;
} catch(e) {
    if (e instanceof UserNotFoundError) {
        // ... Custome error handling for this type of errors.
    }
    // ... Some other code;
}

This lead to a problem that using custom error classes creates ton of if statements if you have
custom error handling for each class of errors. So goal of this package to solve this problem by
adding a function wrapper that will handle overloading for catch statements in nice and convenient way.

Try

This function is a wrapper for that adds a way to overload catch statements.
It will return a function that has method catch on it. You can chain the .catch calls
to add an error handler for a particular type of error object. Also, you can add generic
error handler that will be called if an error was not handled by specific error handlers.
The function fill return the result returned either by the function passed to the Try function call
or by an error handler, or undefined if no value was returned.

const { Try } = require('try-catch-overloaded');

const result = Try(() => {
    // ... do something here.
    return 'Result';
}).catch(UserNotFoundError, (error) => {
    // ... Handle user not found error.
    return 'Result for User Not Found Error';
}).catch(BadArgumentsError, (error) => {
    // ... Handle bad arguments error.
}).catch((error) => {
    // ... Handle every other error that does not matches previus handlers
// Note here you should add () to execute returned function.
})();

console.log(result); // either 'Result' or 'Result for User Not Found Error' or undefined;

If it can not find appropriate error handler it will just throw the error to outer world.

try {
    Try(() => {
    })();
} catch(missConfiguredExecutorError) {
    // It will throw an error if you don't specify at least one error handler. 
}

Dynamic configuration

Because Try returns a function you can always add dynamic configuration for catch methods. Though
I personally don't see a practical application of this, but maybe someone will find this useful. \

Voice of caution: Don't use this feature, you should always define all possible error handlers, otherwise
your code is unsafe because you can miss an error due to misconfiguration. Though, you can always rely on generic error handler.

const executor = Try(() => {
    // ... some code
});

executor.catch(AlwaysAppearingError, (error) => {
    // Handle error/
})

if (needToHandleUserError) {
    executor.catch(UserError, (userError) => {
        // ... handle error.
    })
}

if (needToHandleRandomError) {
    executor.catch(RandomError, (randomError) => {
        // ... handle error;
    });
}

TODO List

  • Migrate this to TS and write types.
  • Add handlers for Async code.
  • Write better documentation.
  • Write example for usage as a callback.
  • Add parameters validation