Skip to content

optimize: include response body in APIError for better error handling#20

Open
zbysir wants to merge 1 commit into
meguminnnnnnnnn:masterfrom
zbysir:master
Open

optimize: include response body in APIError for better error handling#20
zbysir wants to merge 1 commit into
meguminnnnnnnnn:masterfrom
zbysir:master

Conversation

@zbysir

@zbysir zbysir commented Mar 31, 2026

Copy link
Copy Markdown

Added a Body field to the APIError struct to store the response body from HTTP errors. This enhancement allows for more detailed error reporting when handling API responses.

Motivation

Many providers (e.g., OpenRouter) customize their error messages rather than using only Code, Message. Therefore, preserving the original body is necessary.

An example:
When using OpenRouter with OpenAI, an error occurs and returns a very brief but useless error message:
400 Bad Request, message: Provider returned error.

OpenRouter documentation shows errors like this:

type ErrorResponse = {
  error: {
    code: number;
    message: string;
+   metadata?: Record<string, unknown>;
  };
};

Adding a metadata field in this project is not a good choice.
The simpler approach is to expose the body so developers can retrieve the full underlying error via the original body.

Added a Body field to the APIError struct to store the response body from HTTP errors. This enhancement allows for more detailed error reporting when handling API responses.
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