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

network-error-handling

v1.1.5

Published

An useful error handler for axios network requests in a React application

Downloads

12

Readme

Network Error Handling

Introduction

A powerful error handling to manage network axios response errors.

Installation

npm install network-error-handling
yarn add network-error-handling
pnpm install network-error-handling

Usage

Network error handling

  1. Import
import { networkErrorHandling } from "network-error-handling";
  1. Call the function, example:

IMPORTANT: The error parameter is the AxiosError object from the axios response.

networkErrorHandling(error)
  .addError(
    409,
    "User already exists",
    "The user already exists, please try again with another email.",
    () => console.log("Error while creating user")
  )
  .handle();

Using toast

If you plan to display a toast of error, you will need to have a function to handle the toast, example using Shadcn UI toast component

import { toast } from "shadcn-components-folder-location/use-toast";

export const handleErrorToast = (title: string, description: string) =>
  toast({ title, description, variant: "error" });

After this, you can use the withToast method to handle the toast, example:

networkErrorHandling(error)
  .addError(
    400,
    "Invalid form data",
    "The form data is invalid, please check the fields."
  )
  .addError(
    409,
    "User already exists",
    "The user already exists, please try again with another email."
  )
  .withToast(handleErrorToast)
  .handle();

Ooh, you can also use a callback function when using a toast!

networkErrorHandling(error)
  .addError(
    409,
    "User already exists",
    "The user already exists, please try again with another email.",
    () => console.log("Error while creating user")
  )
  .withToast(handleErrorToast)
  .handle();

Status Codes

This package provides a list of status codes that you can use to handle the errors, example:

import { StatusCode } from "network-error-handling";

networkErrorHandling(error)
  .addError(
    StatusCode.CONFLICT,
    "User already exists",
    "The user already exists, please try again with another email."
  )
  .handle();

Api References

networkErrorHandling(error: AxiosError)

  • error: The AxiosError object from the axios response.

addError(statusCode: number, title: string, description: string, callback?: () => void)

  • statusCode: The status code to handle the error.
  • title: The title of the error.
  • description: The description of the error.
  • callback: Optional callback function to execute when the error is handled.

CAUTION: For now, the callback parameter is not async aware, so you can't use async functions inside the callback.

withToast(toastFunction: (title: string, description: string) => void)

  • toastFunction: The function to handle the toast.

Observation: You should handle the toast system by yourself in your project.

handle()

  • Handle the error.

License

This project is licensed under the MIT License - see the LICENSE file for details.