Class MistralProviderErrorMapper

java.lang.Object
com.tnsai.llm.errors.MistralProviderErrorMapper
All Implemented Interfaces:
ProviderErrorMapper

public class MistralProviderErrorMapper extends Object implements ProviderErrorMapper
ProviderErrorMapper for Mistral's chat completions API.

Mistral uses two slightly different error envelope shapes depending on the API version and error category. The "official" shape mirrors OpenAI's nested envelope:


 {
   "object": "error",
   "message": "...",
   "type": "invalid_request_error",
   "param": null,
   "code": "model_not_found"
 }
 

Some endpoints (and older API versions) flatten everything to a top-level message + request_id, so the parser tries both shapes — top-level fields first, then nested error.* as a fallback. Whichever wins, the mapper surfaces message / type / code / param into ProviderDetails.

Native error codes mapped to LLMException.ErrorType:

  • rate_limit_exceeded / requests_limit_exceeded → MODEL_OVERLOADED
  • invalid_api_key / unauthorized → AUTHENTICATION_FAILED
  • model_not_found → MODEL_NOT_FOUND
  • context_length_exceeded / max_tokens_exceeded → CONTEXT_TOO_LONG
  • service_unavailable / service_error → SERVER_ERROR
  • invalid_request_error → INVALID_REQUEST
  • HTTP 5xx → SERVER_ERROR; 401/403 → AUTHENTICATION_FAILED; 429 → MODEL_OVERLOADED; 4xx → INVALID_REQUEST

Captured headers: retry-after, x-request-id, x-ratelimit-*, mistral-correlation-id.

Since:
0.5.3 (issue #87 follow-up — fifth pilot mapper)
  • Constructor Details

    • MistralProviderErrorMapper

      public MistralProviderErrorMapper()
  • Method Details

    • providerId

      public String providerId()
      Specified by:
      providerId in interface ProviderErrorMapper
      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: ProviderErrorMapper
      Translate a raw HTTP error response into a typed LLMException.
      Specified by:
      map in interface ProviderErrorMapper
      Parameters:
      httpStatus - HTTP status code from the response
      responseHeaders - selected response headers (lower-cased keys); empty map if none captured
      responseBody - raw response body; mapper parses provider-specific envelope (e.g. JSON error.code)
      model - model identifier from the request, for the resulting LLMException.getModel()
      cause - underlying transport throwable, may be null when the failure is purely a non-2xx response
      Returns:
      typed LLMException with ProviderDetails attached
    • mapErrorType

      public Optional<com.tnsai.exceptions.LLMException.ErrorType> mapErrorType(String providerCode)
      Description copied from interface: ProviderErrorMapper
      Map a provider-native error code to a framework LLMException.ErrorType. Implementations should err on the side of LLMException.ErrorType.UNKNOWN for codes they don't recognize so that a new provider error doesn't fail the whole mapping path.
      Specified by:
      mapErrorType in interface ProviderErrorMapper
      Parameters:
      providerCode - native code (e.g. "context_length_exceeded")
      Returns:
      matching LLMException.ErrorType, or Optional.empty() if the mapper has no opinion