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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@j31/tryx-handler

v0.3.0

Published

A lightweight error handling package for JavaScript and TypeScript.

Downloads

157

Readme

TryX

A package to simplify error handling and make fetching more predictable and easy to work with.

Install package

Run the command below to install the package

npm i @j31/tryx-handler

How to use the package

To use this package some basic configuration is required

Add cofig

To setup a new TryX instance you should do something like this:

const tx = new TryX({
  timeout: 5000,
});

This sets up an instance to perform actions with. A timeout is required. Since we do not want to take control over the max duration before bailig out this configuration is required to be setup for every instance. Above example would abort after 5 seconds.

See the examples folder for additional configuration options or read through the options below.

timeout

The max duration of an async task executed. This config is required.

logErrors

Choose if and how you would like errors to be logged to the console. Options are:

  • Always
  • Only in development
  • Never (default)

onError

A callback that passes the error when it occurs anywhere in your code. This callback can be used to log the error to an external service.

Fetch data

After completing the config you can use your instance to fetch some data.

const { data, error } = await tx.fetch('api.some-company.com/data');

Note that data and an error are returned and ready for you to be handled. For example

const { data, error } = await tx.fetch('api.some-company.com/data');

if (error) {
  // Handle your error as you wish
  toast.error(error.message);
  // Block any further actions
  return;
}

// If above was not executed that must mean we can continue with the data
console.log('Data:', data);

Adding types

You can add a type to your returned data by passing it to the fetch. For example:

const { data, error } = await tx.fetch<Todo>('api.some-company.com/data');

data will now be typed as a Todo

Overriding the return names

In some cases you might get a conflict with one of the constants. For example, if data was already defined in your file. In this case you can overrride the names like so:

const { data: someNewDataName, error: someNewErrorName } = await tx.fetch(
  'api.some-company.com/data'
);

This will resolve the conflict and your data now becomes accessible under the newly assigned name.

Or alternatively you can define your own name and derive the data and error from that constant.

const myResponseName = await tx.fetch('api.some-company.com/data');

Safely execute (async) functions

As well as fetching data you can also use this package to safely perform function executions. Using this method works roughly the same as the fetch method. To safely execute a function use the execute method like so:

const { data, error } = tx.execute(someFunction);

Or with an async function

const { data, error } = await tx.executeAsync(someAsyncFunction);

Doing so will wrap the function in a try catch block just like the fetch method. The error is ready for you to be handled as you wish.

Run a function with params

If a function takes params you can pass the function like so:

const { data, error } = tx.execute(() => devide(3, 4));

Handling the data

If the functions returns some data, data will be typed automatically. If the function rturns void you can ignore the data object and just check for errors. For example:

const { error } = tx.execute(() => downdload(myFile));

if (error) {
  toast.error('File download failed', {
    description: error.message,
  });
}

If the download function just performs some logic we can leave out the data and just handle the error if it exsists.

Recommended structure

Although you are free to use the package however you like we do have some recommendations.

Store you instance in a standalone file

Although you could create an instance in any file you would like to use the package, we recommend you set it up in a seperate file and export the ready to use constant. For example:

// in /lib/tx.ts

export const tx = new TryX({
  timeout: 5000,
})

// in /some-path/index.ts
import { tx } from '@/lib/tx'

const { data, error } = tx.fetch(...)

This will ensure you just have to maintain one instance instead of multiple.

Development of this package

Run an example

Run below command to run an example

npx tsx examples/baseExample

Tests

To run all tests run below command

npm run test