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#
sealed abstract class AnthropicError(message: String, cause: Throwable) extends Exception(message, cause)
AnthropicError.Api#
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:
BadRequest: 400Authentication: 401Billing: 402PermissionDenied: 403NotFound: 404Conflict: 409RequestTooLarge: 413UnprocessableEntity: 422RateLimited: 429, with the server'sretryAfterwhen it sent oneOverloaded: 529Server: any other 5xxUnexpectedStatus: anything else
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#
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)
Connection: the request did not reach the API.Timeout: no reply within the timeout.Decoding: a reply the SDK could not read.StreamFailure: an error event in the middle of a stream.StreamTruncated: a stream that ended before its last event.
Raised before sending#
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)
StreamingRequired: acreatecall expected to outlast the timeout; usestream.InvalidSchema: a type or tool with no strict JSON Schema, with every issue listed.Configuration: missing or invalid settings, such as no credentials in the environment.
OutputParse#
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
- Connection failures, timeouts, 408, 409, 429 and 5xx are retried first, as
RetryPolicydescribes. You see the error after the last attempt.
Usage#
Recovering from errors#
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.