UNPKG

express-ai-error-handler

Version:
86 lines (56 loc) 3.23 kB
# Express AI Error Handler # [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/David-patrick-chuks/Express-AI-error-handler/blob/main/LICENCE) [![Blazing Fast](https://badgen.now.sh/badge/speed/blazing%20%F0%9F%94%A5/green)](https://npm.im/express-ai-error-handler) [![gzip size](http://img.badgesize.io/https://unpkg.com/express-ai-error-handler@1.0.0/build/src/app.js?compression=gzip)](https://unpkg.com/express-ai-error-handler@1.0.0/build/src/app.js) [![Stable Release](https://img.shields.io/npm/v/express-ai-error-handler.svg)](https://npm.im/express-ai-error-handler) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/David-patrick-chuks/Express-AI-error-handler/blob/main/CONTRIBUTING.md) ## 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: ```sh npm i express-ai-error-handler ``` ## Usage Import and integrate **AI Error Handler** in your Express application: ```ts 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: ```ts 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](https://github.com/David-patrick-chuks)