anthropic4s
GitHub

Reference Errors

AnthropicError

Every failure the SDK raises in F is a subtype of AnthropicError. Each failure mode is its own class, so handling one is a pattern match.

Reference#

AnthropicError#

core · AnthropicError.scala
sealed abstract class AnthropicError(message: String, cause: Throwable) extends Exception(message, cause)

AnthropicError.Api#

core · AnthropicError.scala
sealed abstract class Api(
    val status: Int,
    val errorType: String,
    val errorMessage: String,
    val requestId: Option[String],
    val body: String
) extends AnthropicError(
      s"$status $errorType: $errorMessage" + requestId.fold("")(id => s" (request id: $id)"),
      null
    )

An HTTP error from the API. Quote requestId in a support request. Each status has its own subclass:

core · AnthropicError.scala
final class RateLimited private[anthropic4s] (
    t: String,
    m: String,
    id: Option[String],
    b: String,
    val retryAfter: Option[FiniteDuration]
) extends Api(429, t, m, id, b)

Transport and streaming#

core · AnthropicError.scala
final class Connection private[anthropic4s] (cause: Throwable)
    extends AnthropicError(s"connection error: ${cause.getMessage}", cause)
final class Timeout private[anthropic4s] (val after: FiniteDuration)
    extends AnthropicError(s"request timed out after $after", null)
final class Decoding private[anthropic4s] (val detail: String, val body: String, cause: Throwable)
    extends AnthropicError(s"could not decode the API response: $detail", cause)
final class StreamFailure private[anthropic4s] (val errorType: String, val errorMessage: String)
    extends AnthropicError(s"stream failed with $errorType: $errorMessage", null)
final class StreamTruncated private[anthropic4s] ()
    extends AnthropicError("the stream ended before message_stop", null)

Raised before sending#

core · AnthropicError.scala
final class StreamingRequired private[anthropic4s] (val expected: FiniteDuration, val limit: FiniteDuration)
    extends AnthropicError(
      s"this request may take about $expected, longer than the $limit non-streaming limit; " +
        "stream it (messages.stream(...).finalMessage) or set RequestOptions.withTimeout explicitly",
      null
    )
final class InvalidSchema private[anthropic4s] (val issues: NonEmptyList[SchemaIssue])
    extends AnthropicError("invalid schema: " + issues.map(_.message).toList.mkString("; "), null)
final class Configuration private[anthropic4s] (val detail: String) extends AnthropicError(detail, null)

OutputParse#

core · AnthropicError.scala
final class OutputParse private[anthropic4s] (val detail: String, val reply: Message)
    extends AnthropicError(s"could not read the structured output: $detail", null)

Raised by parse when the reply cannot become an A. The reply is kept so you can see why.

Caveats

Usage#

Recovering from errors#

core · AnthropicError.scala
client.messages.create(request).recoverWith {
  case e: AnthropicError.RateLimited => IO.sleep(e.retryAfter.getOrElse(5.seconds)) *> fallback
  case e: AnthropicError.Api         => IO.println(e.errorMessage) *> fallback
}

Errors, retries and timeouts has the full picture.

Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.