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 🙏

© 2026 – Pkg Stats / Ryan Hefner

response-error-handler

v1.0.5

Published

[![npm version](https://badge.fury.io/js/response-error-handler.svg)](https://badge.fury.io/js/response-error-handler) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Downloads

20

Readme

Response API Error Handler

npm version License: MIT

response-error-handler is an npm package designed to centralize and simplify API error handling in your applications.

Features

  • HTTP Error Handling: Supports common HTTP error codes (400, 401, 403, 404, 500, etc.).
  • GraphQL Error Handling: Handles errors returned by GraphQL APIs.
  • Network Error Handling: Detects internet connection issues.
  • Callbacks: Execute specific functions when errors occur (e.g., redirect users or show notifications).

📦 Installation

Install the package via npm:

npm install response-error-handler

or

npm i response-error-handler

🚀 Usage

  1. Import and Initialize the ApiErrorHandler class:

import ApiErrorHandler from 'response-error-handler';

const callbacks = {
    401: () => console.log("Redirecting to login..."),
    404: () => console.log("Resource not found!"),
    onDefault: () => console.log("An error occurred!"),
};

const errorHandler = new ApiErrorHandler("en", callbacks);
  1. Handling Errors

try {
    // Example with Axios
    const response = await axios.get('/api/data');
    console.log(response.data);
} catch (error) {
    const errorMessage = errorHandler.handle(error);
    console.error(errorMessage); // Displays a user-friendly error message
}

Configuration

Supported Languages

The package supports multiple languages. By default, the language is English ("en"). You can pass another language during initialization like french ("fr") or espanol ("es"):

const errorHandler = new ApiErrorHandler("fr"); 

Callbacks

You can define specific functions to execute when an error occurs. For example, you can redirect users to a login page when a 401 error is received:

const callbacks = {
    401: () => console.log("Redirecting to login..."),
    404: () => console.log("Resource not found!"),
    onDefault: () => console.log("An error occurred!"),
};

const errorHandler = new ApiErrorHandler("en", callbacks);

Full Examples

Example with Axios:

import axios from 'axios';
import ApiErrorHandler from 'response-error-handler';

const errorHandler = new ApiErrorHandler("en");

axios.get('/api/data')
    .then(response => console.log(response.data))
    .catch(error => {
        const errorMessage = errorHandler.handle(error);
        console.error(errorMessage); // Displays a user-friendly error message
    });

Example with GraphQL:

import { ApolloError } from '@apollo/client';
import ApiErrorHandler from 'response-error-handler';

const errorHandler = new ApiErrorHandler("en");

const apolloError = new ApolloError({
    graphQLErrors: [{ message: "GraphQL error occurred." }],
});

const errorMessage = errorHandler.handle(apolloError);
console.error(errorMessage); // "GraphQL error occurred."

👨‍💻 Contribute

Contributions are welcome! Feel free to submit a pull request or open an issue if you have any suggestions or feedback.

  1. Clone the repository:
git clone https://github.com/adelekekismath/response-error-handler.git
  1. Install dependencies:
npm install
  1. Make your changes and run tests:
npm test
  1. Submit a pull request!

Author

Kismath ADELEKE - Your GitHub