Class OpenRouterProviderErrorMapper
- All Implemented Interfaces:
ProviderErrorMapper
ProviderErrorMapper for OpenRouter — eighth pilot mapper.
OpenRouter's envelope is OpenAI-compatible at the wrapper but
extends with a metadata object that carries the upstream
provider's raw response (since OpenRouter is a routing layer in
front of many model providers):
{
"error": {
"code": 429, // numeric HTTP-style or string code
"message": "Rate limited",
"metadata": {
"raw": "...", // upstream provider's raw error
"provider_name": "anthropic" // which upstream returned the error
}
}
}
Two parser branches: error.code can be either a numeric
status code (in which case HTTP routing applies) or a string code
(OpenRouter passes through provider-native codes for some
conditions). Mapper handles both shapes; numeric values land in
ProviderDetails.httpStatus(), string values in
ProviderDetails.providerCode().
Upstream-provider name from metadata.provider_name is
surfaced as ProviderDetails.providerType() so consumers
with a fallback chain can route on "the upstream Anthropic was
rate-limited; try a different OpenRouter route".
Status mappings (string-code branch):
rate_limit_exceeded→ MODEL_OVERLOADEDcontext_length_exceeded→ CONTEXT_TOO_LONGmodel_not_found→ MODEL_NOT_FOUNDinvalid_api_key→ AUTHENTICATION_FAILEDinsufficient_credits→ MODEL_OVERLOADED (OpenRouter- specific: caller's balance is too low to route)
Captured headers: retry-after, x-ratelimit-*,
x-or-request-id, openrouter-model-id.
- Since:
- 0.5.3 (issue #87 follow-up — eighth pilot mapper)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncom.tnsai.exceptions.LLMExceptionmap(int httpStatus, Map<String, String> responseHeaders, String responseBody, String model, Throwable cause) Translate a raw HTTP error response into a typedLLMException.Optional<com.tnsai.exceptions.LLMException.ErrorType> mapErrorType(String providerCode) Map a provider-native error code to a frameworkLLMException.ErrorType.
-
Constructor Details
-
OpenRouterProviderErrorMapper
public OpenRouterProviderErrorMapper()
-
-
Method Details
-
providerId
- Specified by:
providerIdin interfaceProviderErrorMapper- Returns:
- short, lowercase provider id (e.g.
"openai","anthropic"). Used to route a raw response to the right mapper.
-
map
public com.tnsai.exceptions.LLMException map(int httpStatus, Map<String, String> responseHeaders, String responseBody, String model, Throwable cause) Description copied from interface:ProviderErrorMapperTranslate a raw HTTP error response into a typedLLMException.- Specified by:
mapin interfaceProviderErrorMapper- Parameters:
httpStatus- HTTP status code from the responseresponseHeaders- selected response headers (lower-cased keys); empty map if none capturedresponseBody- raw response body; mapper parses provider-specific envelope (e.g. JSONerror.code)model- model identifier from the request, for the resultingLLMException.getModel()cause- underlying transport throwable, may be null when the failure is purely a non-2xx response- Returns:
- typed
LLMExceptionwithProviderDetailsattached
-
mapErrorType
Description copied from interface:ProviderErrorMapperMap a provider-native error code to a frameworkLLMException.ErrorType. Implementations should err on the side ofLLMException.ErrorType.UNKNOWNfor codes they don't recognize so that a new provider error doesn't fail the whole mapping path.- Specified by:
mapErrorTypein interfaceProviderErrorMapper- Parameters:
providerCode- native code (e.g."context_length_exceeded")- Returns:
- matching
LLMException.ErrorType, orOptional.empty()if the mapper has no opinion
-