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.
- 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
Install via npm:
npm i express-ai-error-handler
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'))
Creates a new instance of AI Error Handler.
-
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 totrue
(default:false
).
-
Returns an Express middleware function that handles errors and provides AI-generated suggestions.
Manually generate an AI-powered response for a given error message.
const errorHandler = new AIErrorHandler(['your-api-key'])
const response = await errorHandler.generateSmartResponse('Invalid API token')
console.log(response)
By default, errors are logged using Winston. You can disable logging by setting disableLogging: true
in options.
MIT
Developed by David Patrick BugHunter.dev