Package org.mockserver.llm
Class LlmErrorBodies
java.lang.Object
org.mockserver.llm.LlmErrorBodies
Pure, deterministic helper that produces the provider-specific
JSON error body real LLM providers return for overload / rate-limit / server
errors. Client SDKs (the OpenAI Python SDK, Anthropic SDK, Google GenAI SDK, …)
parse the
error.type / error.code fields in these bodies to drive
their retry/backoff logic, so emitting the correct shape lets MockServer exercise
that logic faithfully against a mock.
Used by HttpLlmResponseActionHandler when injecting a chaos error
(probabilistic error or a stateful quota breach) on the LLM response path. When
the provider is null or unknown, callers fall back to the generic
{"error":{"type":..,"message":..}} body.
Error kinds
The mapping is keyed by a coarseLlmErrorBodies.Kind derived from the HTTP status and the
caller's intent:
LlmErrorBodies.Kind.OVERLOADED— the provider is temporarily overloaded (Anthropic 529overloaded_error; OpenAI 503server_error; …).LlmErrorBodies.Kind.RATE_LIMIT— a quota / rate limit was exceeded (429; OpenAIrate_limit_exceeded, Anthropicrate_limit_error, …).LlmErrorBodies.Kind.SERVER_ERROR— a generic upstream 5xx (OpenAIserver_error, Anthropicapi_error, …).
Provider body reference
- ANTHROPIC / BEDROCK (Anthropic Messages API errors) —
{"type":"error","error":{"type":"overloaded_error"|"rate_limit_error"|"api_error","message":..}}. Bedrock delivers the Anthropic body unchanged. - OPENAI / OPENAI_RESPONSES / AZURE_OPENAI (OpenAI error envelope) —
{"error":{"message":..,"type":"server_error"|"rate_limit_exceeded","code":..,"param":null}}. - GEMINI (Google API error envelope) —
{"error":{"code":..,"message":..,"status":"UNAVAILABLE"|"RESOURCE_EXHAUSTED"|"INTERNAL"}}. - OLLAMA —
{"error":".."}(a plain message string).
All output is a JSON string; the helper is static, deterministic, and performs no I/O. Messages are short, fixed, benign test fixtures.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbodyFor(Provider provider, LlmErrorBodies.Kind kind, int status, String message) Produce the provider-specific JSON error body for the given provider and kind, ornullwhen the provider isnullor has no specific shape (the caller then uses the generic fallback body).static LlmErrorBodies.KindkindForStatus(Integer status) Derive theLlmErrorBodies.Kindfrom an HTTP status code. 429 →LlmErrorBodies.Kind.RATE_LIMIT; 529 →LlmErrorBodies.Kind.OVERLOADED; any other 5xx →LlmErrorBodies.Kind.SERVER_ERROR; any 4xx (other than 429) →LlmErrorBodies.Kind.SERVER_ERROR(treated as a generic provider error envelope).
-
Method Details
-
kindForStatus
Derive theLlmErrorBodies.Kindfrom an HTTP status code. 429 →LlmErrorBodies.Kind.RATE_LIMIT; 529 →LlmErrorBodies.Kind.OVERLOADED; any other 5xx →LlmErrorBodies.Kind.SERVER_ERROR; any 4xx (other than 429) →LlmErrorBodies.Kind.SERVER_ERROR(treated as a generic provider error envelope). Anullstatus defaults toLlmErrorBodies.Kind.SERVER_ERROR. -
bodyFor
public static String bodyFor(Provider provider, LlmErrorBodies.Kind kind, int status, String message) Produce the provider-specific JSON error body for the given provider and kind, ornullwhen the provider isnullor has no specific shape (the caller then uses the generic fallback body). Themessageis escaped for embedding in a JSON string.statusfeeds the numericcodefield for providers (OpenAI Gemini) that echo the HTTP status in the body.- Parameters:
provider- the LLM provider (may benull)kind- the coarse error kindstatus- the HTTP status being returned (used for bodycodefields)message- a human-readable error message- Returns:
- the JSON error body string, or
nullfor an unknown/null provider
-