Skip to content

fix(handlers): return computed error status/message instead of generic 500#1708

Open
ankit25bcs10610 wants to merge 1 commit into
Portkey-AI:mainfrom
ankit25bcs10610:fix/handlers-error-status
Open

fix(handlers): return computed error status/message instead of generic 500#1708
ankit25bcs10610 wants to merge 1 commit into
Portkey-AI:mainfrom
ankit25bcs10610:fix/handlers-error-status

Conversation

@ankit25bcs10610

Copy link
Copy Markdown

What

Four handlers — completionsHandler, embeddingsHandler, imageGenerationsHandler, imageEditsHandler — compute the correct error status and message in their catch block, then throw it away:

let statusCode = 500;
let errorMessage = 'Something went wrong';

if (err instanceof RouterError) {
  statusCode = 400;
  errorMessage = err.message;
}

return new Response(
  JSON.stringify({
    status: 'failure',
    message: 'Something went wrong', // ⬅️ hardcoded, ignores errorMessage
  }),
  {
    status: 500,                     // ⬅️ hardcoded, ignores statusCode
    ...
  }
);

So a conditional-routing failure (RouterError) that should return 400 + the real reason instead returns an opaque 500 "Something went wrong", making these failures hard to diagnose for callers.

Fix

Use the already-computed errorMessage and statusCode in the response. This matches the sibling handlers that already do the right thing: chatCompletionsHandler, messagesHandler, messagesCountTokensHandler, proxyHandler.

8-line change across the four files; no behavior change for non-RouterError errors (still 500 + "Something went wrong"). npm run format:check passes.

…c 500

completionsHandler, embeddingsHandler, imageGenerationsHandler and
imageEditsHandler each compute `statusCode`/`errorMessage` in their catch
block (setting 400 + the real message for a RouterError), but then
returned a hardcoded `status: 500` and `message: 'Something went wrong'`,
discarding both.

As a result, conditional-routing failures that should surface as a 400
with the actual reason were returned as an opaque 500. The sibling
handlers (chatCompletions, messages, messagesCountTokens, proxy) already
use `message: errorMessage` / `status: statusCode`; align these four with
that behavior.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant