1.0.5 • Published 5 months ago

response-error-handler v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

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

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago