Skip to content

Commit 9eb7385

Browse files
committed
Include provider error in openrouter error
1 parent 9f12d4e commit 9eb7385

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

web/src/llm-api/openrouter.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,25 @@ export class OpenRouterError extends Error {
571571
}
572572
}
573573

574+
/**
575+
* Builds an enhanced error message that includes provider metadata when available.
576+
*/
577+
function buildEnhancedErrorMessage(
578+
baseMessage: string,
579+
metadata?: { raw?: string; provider_name?: string },
580+
): string {
581+
if (!metadata?.raw) {
582+
return baseMessage
583+
}
584+
const providerLabel = metadata.provider_name ?? 'Provider details'
585+
const maxRawLength = 1000
586+
const truncatedRaw =
587+
metadata.raw.length > maxRawLength
588+
? metadata.raw.slice(0, maxRawLength) + '...'
589+
: metadata.raw
590+
return `${baseMessage} [${providerLabel}: ${truncatedRaw}]`
591+
}
592+
574593
/**
575594
* Parses an error response from OpenRouter and returns an OpenRouterError.
576595
*/
@@ -583,14 +602,21 @@ async function parseOpenRouterError(
583602
const parsed = JSON.parse(errorText)
584603
const validated = OpenRouterErrorResponseSchema.safeParse(parsed)
585604
if (validated.success) {
605+
// metadata is not in the schema but OpenRouter includes it for provider errors
606+
const metadata = (parsed as any).error?.metadata as
607+
| { raw?: string; provider_name?: string }
608+
| undefined
609+
const enhancedMessage = buildEnhancedErrorMessage(
610+
validated.data.error.message,
611+
metadata,
612+
)
586613
errorBody = {
587614
error: {
588-
message: validated.data.error.message,
615+
message: enhancedMessage,
589616
code: validated.data.error.code ?? null,
590617
type: validated.data.error.type,
591618
param: validated.data.error.param,
592-
// metadata is not in the schema but OpenRouter includes it for provider errors
593-
metadata: (parsed as any).error?.metadata,
619+
metadata,
594620
},
595621
}
596622
} else {

0 commit comments

Comments
 (0)