express-ai-error-handler
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Express AI Error Handler

GitHub license Blazing Fast gzip size Stable Release PRs Welcome

Overview

Express AI Error Handler is an Express middleware that intelligently handles API errors by providing AI-generated suggestions using Google Generative AI. This package helps developers troubleshoot API errors by offering guidance on the correct request format.

Features

  • AI-powered error handling for Express applications
  • Auto-suggestions for API request formatting
  • Supports multiple API keys with automatic failover
  • Configurable logging with Winston
  • Customizable AI model and response settings

Installation

Install via npm:

npm i express-ai-error-handler

Usage

Import and integrate AI Error Handler in your Express application:

import express from 'express'
import AIErrorHandler from 'express-ai-error-handler'

const app = express()
const errorHandler = new AIErrorHandler(['your-api-key'], {
	defaultMessage: 'Something went wrong. Please try again.',
	model: 'gemini-1.5-flash',
	maxTokens: 100,
	disableLogging: false,
})

app.use(errorHandler.middleware())

app.listen(3000, () => console.log('Server running on port 3000'))

API

new AIErrorHandler(apiKeys, options)

Creates a new instance of AI Error Handler.

Parameters:

  • apiKeys (string[]) – Array of API keys for Google Generative AI (at least one required).
  • options (optional object):
    • defaultMessage (string) – Default message if AI fails to generate a response.
    • model (string) – AI model to use (default: gemini-1.5-flash).
    • maxTokens (number) – Maximum token count in AI response (default: 50).
    • disableLogging (boolean) – Disable logging when set to true (default: false).

middleware()

Returns an Express middleware function that handles errors and provides AI-generated suggestions.

generateSmartResponse(errorMessage: string): Promise<string>

Manually generate an AI-powered response for a given error message.

Example:

const errorHandler = new AIErrorHandler(['your-api-key'])
const response = await errorHandler.generateSmartResponse('Invalid API token')
console.log(response)

Logging

By default, errors are logged using Winston. You can disable logging by setting disableLogging: true in options.

License

MIT

Author

Developed by David Patrick BugHunter.dev

Package Sidebar

Install

npm i express-ai-error-handler

Weekly Downloads

6

Version

1.0.2

License

MIT

Unpacked Size

18.4 kB

Total Files

7

Last publish

Collaborators

  • davidtsx