Refactor error handling to config-driven approach with centralized error rules

Made-with: Cursor
This commit is contained in:
decolua
2026-04-15 11:46:47 +07:00
parent b1288c5064
commit b669b6ffc1
20 changed files with 1056 additions and 991 deletions
+9 -62
View File
@@ -1,4 +1,4 @@
import { ERROR_TYPES, DEFAULT_ERROR_MESSAGES } from "../config/runtimeConfig.js";
import { ERROR_TYPES, DEFAULT_ERROR_MESSAGES } from "../config/errorConfig.js";
/**
* Build OpenAI-compatible error response body
@@ -49,56 +49,17 @@ export async function writeStreamError(writer, statusCode, message) {
await writer.write(encoder.encode(`data: ${JSON.stringify(errorBody)}\n\n`));
}
/**
* Parse Antigravity error message to extract retry time
* Example: "You have exhausted your capacity on this model. Your quota will reset after 2h7m23s."
* @param {string} message - Error message
* @returns {number|null} Retry time in milliseconds, or null if not found
*/
export function parseAntigravityRetryTime(message) {
if (typeof message !== "string") return null;
// Match patterns like: 2h7m23s, 5m30s, 45s, 1h20m, etc.
const match = message.match(/reset after (\d+h)?(\d+m)?(\d+s)?/i);
if (!match) return null;
let totalMs = 0;
// Extract hours
if (match[1]) {
const hours = parseInt(match[1]);
totalMs += hours * 60 * 60 * 1000;
}
// Extract minutes
if (match[2]) {
const minutes = parseInt(match[2]);
totalMs += minutes * 60 * 1000;
}
// Extract seconds
if (match[3]) {
const seconds = parseInt(match[3]);
totalMs += seconds * 1000;
}
return totalMs > 0 ? totalMs : null;
}
/**
* Parse upstream provider error response
* @param {Response} response - Fetch response from provider
* @param {string} provider - Provider name (for Antigravity-specific parsing)
* @returns {Promise<{statusCode: number, message: string, retryAfterMs: number|null}>}
* @returns {Promise<{statusCode: number, message: string}>}
*/
export async function parseUpstreamError(response, provider = null) {
export async function parseUpstreamError(response) {
let message = "";
let retryAfterMs = null;
try {
const text = await response.text();
// Try parse as JSON
try {
const json = JSON.parse(text);
message = json.error?.message || json.message || json.error || text;
@@ -112,15 +73,9 @@ export async function parseUpstreamError(response, provider = null) {
const messageStr = typeof message === "string" ? message : JSON.stringify(message);
const finalMessage = messageStr || DEFAULT_ERROR_MESSAGES[response.status] || `Upstream error: ${response.status}`;
// Parse Antigravity-specific retry time from error message
if (provider === "antigravity" && response.status === 429) {
retryAfterMs = parseAntigravityRetryTime(finalMessage);
}
return {
statusCode: response.status,
message: finalMessage,
retryAfterMs
message: finalMessage
};
}
@@ -128,23 +83,15 @@ export async function parseUpstreamError(response, provider = null) {
* Create error result for chatCore handler
* @param {number} statusCode - HTTP status code
* @param {string} message - Error message
* @param {number|null} retryAfterMs - Optional retry-after time in milliseconds
* @returns {{ success: false, status: number, error: string, response: Response, retryAfterMs?: number }}
* @returns {{ success: false, status: number, error: string, response: Response }}
*/
export function createErrorResult(statusCode, message, retryAfterMs = null) {
const result = {
export function createErrorResult(statusCode, message) {
return {
success: false,
status: statusCode,
error: message,
response: errorResponse(statusCode, message)
};
// Add retryAfterMs if available (for Antigravity quota errors)
if (retryAfterMs) {
result.retryAfterMs = retryAfterMs;
}
return result;
}
/**