Class CohereProviderErrorMapper

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

public class CohereProviderErrorMapper extends Object implements ProviderErrorMapper
ProviderErrorMapper for Cohere — tenth pilot mapper.

Cohere's error envelope is intentionally minimal:


 { "message": "API key is invalid" }
 

No structured code / type / param fields. Classification is primarily HTTP-status driven, with a message-text heuristic for the common cases (token-overflow, rate-limit phrasing, model- not-found). Cohere's API style mirrors a request/response orientation (no streaming for chat in the same shape; embed /classify each have their own paths) — this mapper covers the shared error envelope across all of them.

Status mappings:

  • HTTP 401/403 → AUTHENTICATION_FAILED
  • HTTP 429 → MODEL_OVERLOADED
  • HTTP 404 → MODEL_NOT_FOUND
  • HTTP 400 + token-overflow phrase → CONTEXT_TOO_LONG
  • HTTP 4xx → INVALID_REQUEST (fallback)
  • HTTP 5xx → SERVER_ERROR

Captured headers: retry-after, x-request-id (Cohere-specific), x-api-warning (deprecation / upgrade-recommended advisories — useful for forward-looking ops).

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

    • CohereProviderErrorMapper

      public CohereProviderErrorMapper()
  • 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