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

safe-catch

v1.0.12

Published

`SafeCatch` is an NPM library written in TypeScript that provides two utility functions, `tryCatchPromise` and `tryCatchFunction`, to simplify error handling in asynchronous and synchronous code. This library aims to make error handling more concise and r

Downloads

1

Readme

SafeCatch

SafeCatch is an NPM library written in TypeScript that provides two utility functions, tryCatchPromise and tryCatchFunction, to simplify error handling in asynchronous and synchronous code. This library aims to make error handling more concise and readable by encapsulating the try-catch logic within these functions.

Installation

You can install SafeCatch using npm:


npm install safe-catch

Usage

tryCatchPromise

The tryCatchPromise function is designed for handling errors in asynchronous operations wrapped in Promises.

/* imports... */
import { tryCatchPromise } from "safe-catch";

function fetchData() {
    return axios.get('https://jsonplaceholder.typicode.com/todos/1')
}

async function example() {
  const [result, error] = await tryCatchPromise(fetchData());

  if (error) {
    return console.error("An error occurred:", error);
  } 

  return console.log("Data fetched:", await result.data);
  
}

example();

In the example above, tryCatchPromise is used to handle the asynchronous operation fetchData(). It returns a tuple [result, error], where result contains the result of the successful operation or null if an error occurred, and error contains the error object or null if the operation was successful.

tryCatchFunction

The tryCatchFunction is designed for handling errors in synchronous functions.


import { tryCatchFunction } from "safe-catch";

function divide(a: number, b: number) {
  if (b === 0) {
    throw new Error("Division by zero");
  }

  return a / b;
}

function example() {
  const [result, error] = tryCatchFunction(() => divide(10, 2));

  if (error) {
    return console.log('Something went wrong :(', error)
  }

  return console.log('Result', result)
}

example();

In the above example, tryCatchFunction wraps the synchronous function divide. It also returns a tuple [result, error], where result contains the return value of the function if it executed successfully, or null if an error occurred. The error variable holds the error object or null if the function was executed without any errors.

Benefits of Using SafeCatch

  1. Code Simplicity: By encapsulating the try-catch logic within the utility functions, SafeCatch eliminates the need for writing repetitive and verbose try-catch blocks. This leads to cleaner and more readable code.
  2. Type Safety: Built with TypeScript, SafeCatch leverages the static typing capabilities of the language. The utility functions tryCatchPromise and tryCatchFunction allow developers to define the expected return types and error types explicitly. This enables better type checking at compile-time, reducing the chances of runtime errors and providing enhanced code reliability.
  3. Reduced Boilerplate: The library handles the error handling boilerplate code, such as creating try-catch blocks and checking for errors. This allows developers to focus on the core logic of their functions, resulting in more concise and maintainable code.
  4. Consistent Error Handling: SafeCatch promotes a consistent approach to error handling across asynchronous and synchronous code. It provides a uniform way to handle errors, making the codebase easier to understand and maintain.

Conclusion

SafeCatch is a TypeScript library that simplifies error handling in asynchronous and synchronous code by encapsulating the try-catch logic within utility functions. It helps improve code readability, reduces boilerplate, and promotes consistent error handling practices. By using SafeCatch, developers can write cleaner and more maintainable code while effectively handling errors.