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

handled

v1.2.2

Published

async error handling

Downloads

12

Readme


handled

handled is a convenient and simple asynchronous error handler for node.js. You can pass handled a Promise, an Array of Promises, or an async function, and it will return it with basic error handling added. Avoid the dreaded unhandledPromiseRejectionWarning!

handled is set up to use a shortcut, allowing it to be invoked like an object property, using dot-notation. This makes it easy to add error handling without the need for further nesting of code.


Installation

$ npm install handled

Using handled

After installation, you will need to require handled in your file in order to use it. If you would like to access any or all of the methods that comprise the package individually, it is convenient to use a destructuring assignment like so:

const {	handlePromise, handleAll, handleAsyncFn, assignDotShortcut, assignPromiseHandler, assignFnHandler} = require("handled");

The Dot-Notation Shortcut - ø

Though you can import and use the included methods separately, handled is designed to be used with a convenient dot-notation shortcut. By default, the lowercase "slashed-o" symbol– ø is set as the shortcut (Option-o on Mac). To use handled this way, simply require the package, and you are ready to go:

const handled = require("handled");

const examplePromise = Promise.reject("example error message");
const handledPromise = examplePromise.ø;

The shortcut will recognize the type of argument passed, and will work whether it is used on a Promise, array of Promises, or an asynchronous function, by deciding whether to invoke handlePromise(), handleAll(), or handleAsyncFn(), respectively.

You can add a different dot-notation shortcut if you want, by passing the desired shortcut, as a string, to the assignDotShortcut() function thusly:

const {assignDotShortcut} = require("handled");

assignDotShortcut("myShortcut");

const exampleFunction = async function (){
	return await //async stuff
}
const handledFunction = exampleFunction.myShortcut;

API Reference


handlePromise(myPromise, [errorHandler], [finallyCallback])

Alias: myPromise.ø

Accepts a Promise and returns it, chained with a .catch() statement for error handling.

  • myPromise: An Array of Promises to apply the error handler to.
  • errorHandler (optional): a custom error handling function to be used in place of the default.
  • finallyCallback (optional): a callback function to be called in a finally block after the try and/or catch blocks have executed. This callback will run regardless of whether an exception is encountered.

handleAll(promiseArray, [errorHandler], [finallyCallback])

Alias: promiseArray.ø

Accepts an Array of Promises, and returns them in a new Array, with a .catch() statement chained to each of them, in their original order.

  • promiseArray: An Array of Promises to apply the error handler to.
  • errorHandler (optional): a custom error handling function to be used in place of the default.
  • finallyCallback (optional): a callback function to be called in a finally block after the try and/or catch blocks have executed. This callback will run regardless of whether an exception is encountered.

handleAsyncFn(myFunction, [errorHandler], [finallyCallback])

Alias: myFunction.ø

Accepts an async function (or a standard function) and returns a new async function wrapper, which will execute the original function nested within a try/catch block.

  • myFunction: the async function or function to apply the error handler to.
  • errorHandler (optional): a custom error handling function to be used in place of the default.
  • finallyCallback (optional): a callback function to be called in a finally block after the try and/or catch blocks have executed. This callback will run regardless of whether an exception is encountered.

assignDotShortcut(myShortcut)

Allows the user to implement a custom dot-notation shortcut for handled, in addition to the default "slashed-o" (ø) shortcut. This is done by defining a new property on Object.prototype, which invokes a "getter" that identifies the type of input as either a Promise, an Array of Promises, or an async function and invokes the appropriate handled method to handle it. (If the value passed is none of the above, the original input is returned unchanged).

  • myShortcut: a String, to be set as a property name on Object.prototype, and used as a shortcut to call handlePromise(), handleAll(), or handleAsyncFn().

assignPromiseHandler(myErrorHandler)

Allows the user to set a new default error handler function for Promises, to be used in calls to handlePromise() and handleAll(), when the optional errorHandler parameter is not passed.

  • myErrorHandler: an error handling function, that accepts one argument– the Error that has been thrown.

assignPromiseHandler(myErrorHandler)

Allows the user to set a new default error handler function to be used in calls to handleAsyncFn(), when the optional errorHandler parameter is not passed.

  • myErrorHandler: an error handling function, that accepts one argument– the Error that has been thrown.


License

Copyright © 2018 Dennis Hodges

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.