2.0.3 • Published 5 months ago

kareem_custom_express_error_handler v2.0.3

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

Certainly! Here's an enhanced version of the Markdown documentation:

# Kareem Custom Express Error Handler

A robust and customizable error handler for Express applications.

## Installation

Install the package using npm:

```bash
npm install kareem_custom_express_error_handler
```

Usage

  1. Import Dependencies:

    const express = require("express");
    const app = express();
    
    const error_handler = require("kareem_custom_express_error_handler");
  2. Initialize Settings:

    Create an instance of the Settings class to configure the error handler:

    const settings = new error_handler.Settings();
    
    // Set custom settings (optional, uses default settings if not provided)
    settings.set_settings({
      log_errors_to_winston: true,
      winston_logs_directory:
        path.join(__dirname, "logs", "errors") || undefined,
      log_errors_to_db: true,
      db_server: "mongodb",
      uri: "mongodb://localhost:27017",
      password: "x",
      db_name: "logs",
    });
  3. Use Middleware:

    Integrate the error handler middleware into your Express application:

    app.use(error_handler.error_handler);
  4. Throw Custom Errors:

    Utilize the provided error classes (Api_error and Server_error) to handle errors:

    try {
      throw new error_handler.Api_error(
        "error message",
        "client message",
        404,
        0
      );
    } catch (error) {
      // Handle the error here
      next(error);
    }

Error Classes

Api_error

Creates an instance for API-related errors.

Constructor

new error_handler.Api_error(message: any, frontend_message?: string, status?: number, api_response_code?: number);
  • message: The error message.
  • frontend_message (optional): Message sent to the client. Default is "internal server error <'_'>".
  • status (optional): HTTP status code. Default is 500.
  • api_response_code (optional): API response code. Default is 0.

Server_error

Creates a generic server error instance.

Constructor

new error_handler.Server_error(message: any, status?: number, api_response_code?: number);
  • message: The error message.
  • status (optional): HTTP status code. Default is 500.
  • api_response_code (optional): API response code. Default is 0.
This comprehensive Markdown documentation provides clear instructions on installing and using your custom error handler in an Express application, making it easy for developers to integrate and customize error handling.
2.0.3

5 months ago

2.0.2

5 months ago

1.1.0

5 months ago

2.0.1

5 months ago

2.0.0

5 months ago

1.0.0

5 months ago