refactor: enhance error handling and normalization across AI engines

This update introduces a centralized error handling mechanism for various AI engines, improving the consistency and clarity of error messages. The new `normalizeEngineError` function standardizes error responses, allowing for better user feedback and recovery suggestions. Additionally, specific error classes for insufficient credits, rate limits, and service availability have been implemented, along with user-friendly formatting for error messages. This refactor aims to enhance the overall user experience when interacting with the AI services.
This commit is contained in:
di-sukharev
2026-01-17 23:34:49 +03:00
parent 8b0ee25923
commit 5b241ed2d0
16 changed files with 1081 additions and 412 deletions

View File

@@ -5,9 +5,8 @@ import {
HarmCategory,
Part
} from '@google/generative-ai';
import axios from 'axios';
import { OpenAI } from 'openai';
import { ModelNotFoundError } from '../utils/errors';
import { normalizeEngineError } from '../utils/engineErrorHandler';
import { removeContentTags } from '../utils/removeContentTags';
import { AiEngine, AiEngineConfig } from './Engine';
@@ -76,30 +75,7 @@ export class GeminiEngine implements AiEngine {
const content = result.response.text();
return removeContentTags(content, 'think');
} catch (error) {
const err = error as Error;
// Check for model not found errors
if (err.message?.toLowerCase().includes('model') &&
(err.message?.toLowerCase().includes('not found') ||
err.message?.toLowerCase().includes('does not exist') ||
err.message?.toLowerCase().includes('invalid'))) {
throw new ModelNotFoundError(this.config.model, 'gemini', 404);
}
if (
axios.isAxiosError<{ error?: { message: string } }>(error) &&
error.response?.status === 401
) {
const geminiError = error.response.data.error;
if (geminiError) throw new Error(geminiError?.message);
}
// Check axios 404 errors
if (axios.isAxiosError(error) && error.response?.status === 404) {
throw new ModelNotFoundError(this.config.model, 'gemini', 404);
}
throw err;
throw normalizeEngineError(error, 'gemini', this.config.model);
}
}
}