MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / handleProviderError

Function handleProviderError

src/api/providers/utils/error-handler.ts:37–106  ·  view source on GitHub ↗
(
	error: unknown,
	providerName: string,
	options?: {
		/** Custom message prefix (default: "completion") */
		messagePrefix?: string
		/** Custom message transformer */
		messageTransformer?: (msg: string) => string
	},
)

Source from the content-addressed store, hash-verified

35 * }
36 */
37export function handleProviderError(
38 error: unknown,
39 providerName: string,
40 options?: {
41 /** Custom message prefix (default: "completion") */
42 messagePrefix?: string
43 /** Custom message transformer */
44 messageTransformer?: (msg: string) => string
45 },
46): Error {
47 const messagePrefix = options?.messagePrefix || "completion"
48
49 if (error instanceof Error) {
50 const anyErr = error as any
51 const msg = anyErr?.error?.metadata?.raw || error.message || ""
52
53 // Log the original error details for debugging
54 console.error(`[${providerName}] API error:`, {
55 message: msg,
56 name: error.name,
57 stack: error.stack,
58 status: anyErr.status,
59 })
60
61 let wrapped: Error
62
63 // Special case: Invalid character/ByteString conversion error in API key
64 // This is specific to OpenAI-compatible SDKs
65 if (msg.includes("Cannot convert argument to a ByteString")) {
66 wrapped = new Error(i18n.t("common:errors.api.invalidKeyInvalidChars"))
67 } else {
68 // Apply custom transformer if provided, otherwise use default format
69 const finalMessage = options?.messageTransformer
70 ? options.messageTransformer(msg)
71 : `${providerName} ${messagePrefix} error: ${msg}`
72 wrapped = new Error(finalMessage)
73 }
74
75 // Preserve HTTP status and structured details for retry/backoff + UI
76 // These fields are used by Task.backoffAndAnnounce() and ChatRow/ErrorRow
77 // to provide status-aware error messages and handling
78 if (anyErr.status !== undefined) {
79 ;(wrapped as any).status = anyErr.status
80 }
81 if (anyErr.errorDetails !== undefined) {
82 ;(wrapped as any).errorDetails = anyErr.errorDetails
83 }
84 if (anyErr.code !== undefined) {
85 ;(wrapped as any).code = anyErr.code
86 }
87 // Preserve AWS-specific metadata if present (for Bedrock)
88 if (anyErr.$metadata !== undefined) {
89 ;(wrapped as any).$metadata = anyErr.$metadata
90 }
91
92 return wrapped
93 }
94

Callers 4

createMessageMethod · 0.90
handleOpenAIErrorFunction · 0.90
handleOpenAIErrorFunction · 0.85

Calls 2

errorMethod · 0.65
StringInterface · 0.50

Tested by

no test coverage detected