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

Function extractErrorFromMetadataRaw

src/api/providers/openrouter.ts:92–120  ·  view source on GitHub ↗

* Helper function to parse and extract error message from metadata.raw * metadata.raw is often a JSON encoded string that may contain .message or .error fields * Example structures: * - {"message": "Error text"} * - {"error": "Error text"} * - {"error": {"message": "Error text"}} * - {"type":"

(raw: string | undefined)

Source from the content-addressed store, hash-verified

90 * - {"type":"error","error":{"type":"invalid_request_error","message":"tools: Tool names must be unique."}}
91 */
92function extractErrorFromMetadataRaw(raw: string | undefined): string | undefined {
93 if (!raw) {
94 return undefined
95 }
96
97 try {
98 const parsed = JSON.parse(raw)
99 // Check for common error message fields
100 if (typeof parsed === "object" && parsed !== null) {
101 // Check for direct message field
102 if (typeof parsed.message === "string") {
103 return parsed.message
104 }
105 // Check for nested error.message field (e.g., Anthropic error format)
106 if (typeof parsed.error === "object" && parsed.error !== null && typeof parsed.error.message === "string") {
107 return parsed.error.message
108 }
109 // Check for error as a string
110 if (typeof parsed.error === "string") {
111 return parsed.error
112 }
113 }
114 // If we can't extract a specific field, return the raw string
115 return raw
116 } catch {
117 // If it's not valid JSON, return as-is
118 return raw
119 }
120}
121
122// See `OpenAI.Chat.Completions.ChatCompletionChunk["usage"]`
123// `CompletionsAPI.CompletionUsage`

Callers 3

handleStreamingErrorMethod · 0.85
createMessageMethod · 0.85
completePromptMethod · 0.85

Calls 1

parseMethod · 0.80

Tested by

no test coverage detected