HttpErrorMapper

org.llm4s.llmconnect.provider.HttpErrorMapper
@Stable

Shared HTTP status-code to org.llm4s.error.LLMError mapping used by all HTTP-based LLM provider clients.

Centralises the duplicated pattern of converting non-2xx responses into typed Result errors. Provider-specific error details are extracted from the JSON response body when possible and truncated to a safe length.

Attributes

Source
HttpErrorMapper.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def mapStreamError(status: Int, message: String, provider: ProviderId): LLMError

The error a provider reported inside a stream, after the 200 status (RFC 0009, rule 2): mapped as a response with status would be, keeping message (redacted) in every error type, a RateLimitError included, so the caller sees the provider's explanation.

The error a provider reported inside a stream, after the 200 status (RFC 0009, rule 2): mapped as a response with status would be, keeping message (redacted) in every error type, a RateLimitError included, so the caller sees the provider's explanation.

Value parameters

message

the provider's explanation, as the event carried it

provider

the id of the provider that reported it: the provider the error names

status

the HTTP status the event stands for: the one it states, or the one its kind maps to (502 for a failure that states none)

Attributes

Source
HttpErrorMapper.scala
def toError(statusCode: Int, body: String, provider: ProviderId, headers: Map[String, Seq[String]]): LLMError

Maps an HTTP error response to a typed org.llm4s.error.LLMError.

Maps an HTTP error response to a typed org.llm4s.error.LLMError.

Status Error
401, 403 org.llm4s.error.AuthenticationError
429 org.llm4s.error.RateLimitError, with retryAfter from Retry-After or a RetryInfo
400, 413, 422 org.llm4s.error.ContextLengthError when the body says the request does not fit the model's context window
400, 404 org.llm4s.error.ModelNotFoundError when the body says the model is unknown
400 org.llm4s.error.ValidationError
other org.llm4s.error.ServiceError

Every error carries the response's facts: httpStatus is statusCode, requestId the id in the response's headers (x-request-id, request-id, x-amzn-requestid, apim-request-id) when it has one, and retryAfter the delay its Retry-After header asks for, or, on a 429 without one, the retryDelay of a google.rpc.RetryInfo in the body's error.details (Gemini and Vertex AI send the delay there, as "36s").

A 401, 403, 429 or 404 message ends with the next step to take and a link to its entry on the First-Run Troubleshooting page.

A Retry-After header (delta-seconds or an HTTP-date, name matched case-insensitively) becomes the error's retryAfter duration; an absent, unparseable or negative value leaves it unset, and the retry policy chooses the wait.

Value parameters

body

the raw response body (may be JSON or plain text)

headers

the response headers, as carried by org.llm4s.http.HttpResponse; Map.empty only when there is no response to take them from

provider

the id of the provider that answered (e.g. gemini): the provider the error names, in its provider and its message

statusCode

the HTTP status code (must be outside 2xx range)

Attributes

Returns

the appropriate LLMError subtype; a client returns it as a Left

Source
HttpErrorMapper.scala