Repository navigation
fix(gateway): openai-compatible error envelopes #2532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4b5dca1
8d4b25d
6c3bacf
9551f83
b66012d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,10 @@ import { anthropic } from "./anthropic/anthropic.js"; | |
| import { chat } from "./chat/chat.js"; | ||
| import { embeddingsRoute } from "./embeddings/route.js"; | ||
| import { imagesRoute } from "./images/route.js"; | ||
| import { | ||
| buildAnthropicErrorBody, | ||
| buildOpenAIErrorBody, | ||
| } from "./lib/error-response.js"; | ||
| import { mcpHandler, registerMcpOAuthRoutes } from "./mcp/mcp.js"; | ||
| import { tracingMiddleware } from "./middleware/tracing.js"; | ||
| import { models } from "./models/route.js"; | ||
|
|
@@ -35,6 +39,7 @@ import { responses } from "./responses/responses.js"; | |
| import { videosRoute } from "./videos/route.js"; | ||
|
|
||
| import type { ServerTypes } from "./vars.js"; | ||
| import type { Context } from "hono"; | ||
|
|
||
| export const config = { | ||
| servers: [ | ||
|
|
@@ -117,33 +122,34 @@ app.use("*", async (c, next) => { | |
| return await next(); | ||
| }); | ||
|
|
||
| // Renders a gateway-level error in a provider-compatible shape. The Anthropic | ||
| // `/v1/messages` endpoint expects Anthropic's `{ type: "error", error: {...} }` | ||
| // envelope; every other (OpenAI-compatible) endpoint expects OpenAI's | ||
| // `{ error: { message, type, param, code } }` envelope. | ||
| function renderGatewayError( | ||
| c: Context<ServerTypes>, | ||
| status: number, | ||
| message: string, | ||
| ) { | ||
| if (c.req.path.startsWith("/v1/messages")) { | ||
| return c.json(buildAnthropicErrorBody({ message, status }), status as any); | ||
| } | ||
| return c.json(buildOpenAIErrorBody({ message, status }), status as any); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| app.onError((error, c) => { | ||
| if (error instanceof UnsupportedAudioFormatError) { | ||
| logger.warn("Unsupported audio format", { | ||
| message: error.message, | ||
| format: error.format, | ||
| providerTarget: error.providerTarget, | ||
| }); | ||
| return c.json( | ||
| { | ||
| error: true, | ||
| status: 400, | ||
| message: error.message, | ||
| }, | ||
| 400, | ||
| ); | ||
| return renderGatewayError(c, 400, error.message); | ||
| } | ||
|
|
||
| if (error instanceof InvalidFileContentError) { | ||
| logger.warn("Invalid file content", { message: error.message }); | ||
| return c.json( | ||
| { | ||
| error: true, | ||
| status: 400, | ||
| message: error.message, | ||
| }, | ||
| 400, | ||
| ); | ||
| return renderGatewayError(c, 400, error.message); | ||
| } | ||
|
|
||
| if (error instanceof UnsupportedDocumentFormatError) { | ||
|
|
@@ -152,16 +158,7 @@ app.onError((error, c) => { | |
| mimeType: error.mimeType, | ||
| providerTarget: error.providerTarget, | ||
| }); | ||
| return c.json( | ||
| { | ||
| error: true, | ||
| status: 400, | ||
| message: error.message, | ||
| mimeType: error.mimeType, | ||
| providerTarget: error.providerTarget, | ||
| }, | ||
| 400, | ||
| ); | ||
| return renderGatewayError(c, 400, error.message); | ||
| } | ||
|
|
||
| if (error instanceof HTTPException) { | ||
|
|
@@ -173,15 +170,7 @@ app.onError((error, c) => { | |
| logger.warn("HTTP client error", { status, message: error.message }); | ||
| } | ||
|
|
||
| return c.json( | ||
| { | ||
| error: true, | ||
| status, | ||
| message: error.message || "An error occurred", | ||
| ...(error.res ? { details: error.res } : {}), | ||
| }, | ||
| status, | ||
| ); | ||
| return renderGatewayError(c, status, error.message || "An error occurred"); | ||
| } | ||
|
|
||
| // Handle timeout errors (from AbortSignal.timeout) - these are expected | ||
|
|
@@ -192,14 +181,7 @@ app.onError((error, c) => { | |
| path: c.req.path, | ||
| method: c.req.method, | ||
| }); | ||
| return c.json( | ||
| { | ||
| error: true, | ||
| status: 504, | ||
| message: "Gateway Timeout", | ||
| }, | ||
| 504, | ||
| ); | ||
| return renderGatewayError(c, 504, "Gateway Timeout"); | ||
| } | ||
|
|
||
| // Handle client disconnection (AbortError) - the client closed the | ||
|
|
@@ -210,26 +192,12 @@ app.onError((error, c) => { | |
| path: c.req.path, | ||
| method: c.req.method, | ||
| }); | ||
| return c.json( | ||
| { | ||
| error: true, | ||
| status: 499, | ||
| message: "Client Closed Request", | ||
| }, | ||
| 499 as any, | ||
| ); | ||
| return renderGatewayError(c, 499, "Client Closed Request"); | ||
| } | ||
|
|
||
| // For any other errors (non-HTTPException), return 500 Internal Server Error | ||
| logger.error("Unhandled error", toError(error)); | ||
| return c.json( | ||
| { | ||
| error: true, | ||
| status: 500, | ||
| message: "Internal Server Error", | ||
| }, | ||
| 500, | ||
| ); | ||
| return renderGatewayError(c, 500, "Internal Server Error"); | ||
| }); | ||
|
|
||
| const root = createRoute({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { | ||
| buildAnthropicErrorBody, | ||
| buildOpenAIErrorBody, | ||
| getAnthropicErrorType, | ||
| getOpenAIErrorMeta, | ||
| } from "./error-response.js"; | ||
|
|
||
| describe("error-response", () => { | ||
| test("builds OpenAI envelope with status-derived type and code", () => { | ||
| expect( | ||
| buildOpenAIErrorBody({ | ||
| message: "Unauthorized: LLMGateway API key reached its usage limit.", | ||
| status: 401, | ||
| }), | ||
| ).toEqual({ | ||
| error: { | ||
| message: "Unauthorized: LLMGateway API key reached its usage limit.", | ||
| type: "invalid_request_error", | ||
| param: null, | ||
| code: "invalid_api_key", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("maps rate limit and server errors to OpenAI types", () => { | ||
| expect(getOpenAIErrorMeta(429)).toEqual({ | ||
| type: "rate_limit_error", | ||
| code: "rate_limit_exceeded", | ||
| }); | ||
| expect(getOpenAIErrorMeta(500)).toEqual({ type: "api_error", code: null }); | ||
| expect(getOpenAIErrorMeta(400)).toEqual({ | ||
| type: "invalid_request_error", | ||
| code: null, | ||
| }); | ||
| }); | ||
|
|
||
| test("allows explicit type/code/param overrides", () => { | ||
| expect( | ||
| buildOpenAIErrorBody({ | ||
| message: "bad input", | ||
| status: 400, | ||
| code: "invalid_json", | ||
| param: "messages", | ||
| }), | ||
| ).toEqual({ | ||
| error: { | ||
| message: "bad input", | ||
| type: "invalid_request_error", | ||
| param: "messages", | ||
| code: "invalid_json", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("builds Anthropic envelope with status-derived type", () => { | ||
| expect(buildAnthropicErrorBody({ message: "denied", status: 403 })).toEqual( | ||
| { | ||
| type: "error", | ||
| error: { type: "permission_error", message: "denied" }, | ||
| }, | ||
| ); | ||
| expect(getAnthropicErrorType(429)).toBe("rate_limit_error"); | ||
| expect(getAnthropicErrorType(500)).toBe("api_error"); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.