11import { t } from "../../../i18n"
2+ import { serializeError } from "serialize-error"
23
34/**
45 * HTTP error interface for embedder errors
@@ -42,31 +43,6 @@ export function getErrorMessageForStatus(status: number | undefined, embedderTyp
4243 }
4344}
4445
45- /**
46- * Maps connection error messages to appropriate error messages
47- */
48- export function getErrorMessageForConnectionError ( errorMessage : string | undefined ) : string | undefined {
49- if ( ! errorMessage ) return undefined
50-
51- if ( errorMessage . includes ( "ENOTFOUND" ) || errorMessage . includes ( "ECONNREFUSED" ) ) {
52- return "embeddings:validation.connectionFailed"
53- }
54-
55- if ( errorMessage . includes ( "ETIMEDOUT" ) || errorMessage === "AbortError" ) {
56- return "embeddings:validation.connectionFailed"
57- }
58-
59- if ( errorMessage . includes ( "Failed to parse response JSON" ) ) {
60- return "embeddings:validation.invalidResponse"
61- }
62-
63- if ( errorMessage . includes ( "HTTP 0:" ) || errorMessage === "No response" ) {
64- return "embeddings:validation.connectionFailed"
65- }
66-
67- return undefined
68- }
69-
7046/**
7147 * Extracts status code from various error formats
7248 */
@@ -85,6 +61,11 @@ export function extractStatusCode(error: any): number | undefined {
8561 }
8662 }
8763
64+ // Use serialize-error as fallback for complex objects
65+ const serialized = serializeError ( error )
66+ if ( serialized ?. status ) return serialized . status
67+ if ( serialized ?. response ?. status ) return serialized . response . status
68+
8869 return undefined
8970}
9071
@@ -108,6 +89,12 @@ export function extractErrorMessage(error: any): string {
10889 }
10990 }
11091
92+ // Use serialize-error as fallback for complex objects
93+ const serialized = serializeError ( error )
94+ if ( serialized ?. message ) {
95+ return serialized . message
96+ }
97+
11198 return "Unknown error"
11299}
113100
@@ -122,15 +109,18 @@ export function handleValidationError(
122109 beforeStandardHandling ?: ( error : any ) => { valid : boolean ; error : string } | undefined
123110 } ,
124111) : { valid : boolean ; error : string } {
125- // Allow custom handling first
112+ // Serialize the error to ensure we have access to all properties
113+ const serializedError = serializeError ( error )
114+
115+ // Allow custom handling first (pass original error for backward compatibility)
126116 if ( customHandlers ?. beforeStandardHandling ) {
127117 const customResult = customHandlers . beforeStandardHandling ( error )
128118 if ( customResult ) return customResult
129119 }
130120
131- // Extract status code and error message
132- const statusCode = extractStatusCode ( error )
133- const errorMessage = extractErrorMessage ( error )
121+ // Extract status code and error message from serialized error
122+ const statusCode = extractStatusCode ( serializedError )
123+ const errorMessage = extractErrorMessage ( serializedError )
134124
135125 // Check for status-based errors first
136126 const statusError = getErrorMessageForStatus ( statusCode , embedderType )
@@ -139,9 +129,21 @@ export function handleValidationError(
139129 }
140130
141131 // Check for connection errors
142- const connectionError = getErrorMessageForConnectionError ( errorMessage )
143- if ( connectionError ) {
144- return { valid : false , error : connectionError }
132+ if ( errorMessage ) {
133+ if (
134+ errorMessage . includes ( "ENOTFOUND" ) ||
135+ errorMessage . includes ( "ECONNREFUSED" ) ||
136+ errorMessage . includes ( "ETIMEDOUT" ) ||
137+ errorMessage === "AbortError" ||
138+ errorMessage . includes ( "HTTP 0:" ) ||
139+ errorMessage === "No response"
140+ ) {
141+ return { valid : false , error : "embeddings:validation.connectionFailed" }
142+ }
143+
144+ if ( errorMessage . includes ( "Failed to parse response JSON" ) ) {
145+ return { valid : false , error : "embeddings:validation.invalidResponse" }
146+ }
145147 }
146148
147149 // For generic errors, preserve the original error message if it's not a standard one
@@ -183,31 +185,3 @@ export function formatEmbeddingError(error: any, maxRetries: number): Error {
183185 return new Error ( t ( "embeddings:failedWithError" , { attempts : maxRetries , errorMessage } ) )
184186 }
185187}
186-
187- /**
188- * Checks if an error is a rate limit error
189- */
190- export function isRateLimitError ( error : any ) : boolean {
191- const httpError = error as HttpError
192- return httpError ?. status === 429
193- }
194-
195- /**
196- * Logs retry attempt for rate limit errors
197- */
198- export function logRateLimitRetry ( delayMs : number , attempt : number , maxRetries : number ) : void {
199- console . warn (
200- t ( "embeddings:rateLimitRetry" , {
201- delayMs,
202- attempt,
203- maxRetries,
204- } ) ,
205- )
206- }
207-
208- /**
209- * Logs embedding error with context
210- */
211- export function logEmbeddingError ( embedderName : string , error : any , attempt : number , maxRetries : number ) : void {
212- console . error ( `${ embedderName } embedder error (attempt ${ attempt } /${ maxRetries } ):` , error )
213- }
0 commit comments