rag_toolchain/clients/anthropic/model/
errors.rs

1use serde::Deserialize;
2use thiserror::Error;
3
4#[derive(Debug, Deserialize, PartialEq, Clone)]
5pub struct AnthropicErrorBody {
6    pub r#type: String,
7    pub error: AnthropicErrorDetails,
8}
9
10#[derive(Debug, Deserialize, PartialEq, Clone)]
11pub struct AnthropicErrorDetails {
12    pub r#type: String,
13    pub message: String,
14}
15
16/// # [` AnthropicError`]
17///
18/// This error type largely mirrors the error codes list here
19/// <https://docs.anthropic.com/en/api/errors>.
20#[derive(Error, Debug, PartialEq, Clone)]
21pub enum AnthropicError {
22    /// # There was an issue with the format or content of your request.
23    /// # We may also use this error type for other 4XX status codes not listed below.
24    #[error("Invalid Request Error: {0:?}")]
25    CODE400(AnthropicErrorBody),
26    /// # There’s an issue with your API key
27    #[error("Authentication Error: {0:?}")]
28    CODE401(AnthropicErrorBody),
29    /// # Your API key does not have permission to use the specified resource.
30    #[error("Permission Error: {0:?}")]
31    CODE403(AnthropicErrorBody),
32    /// # The requested resource was not found.
33    #[error("Not Found Error: {0:?}")]
34    CODE404(AnthropicErrorBody),
35    /// # Request exceeds the maximum allowed number of bytes.
36    #[error("Request Too Large Error: {0:?}")]
37    CODE413(AnthropicErrorBody),
38    /// # Your account has hit a rate limit.
39    #[error("Rate Limit Error: {0:?}")]
40    CODE429(AnthropicErrorBody),
41    /// # An unexpected error has occurred internal to Anthropic’s systems
42    #[error("API Error: {0:?}")]
43    CODE500(AnthropicErrorBody),
44    /// # Anthropic’s API is temporarily overloaded.
45    #[error("Overloaded Error: {0:?}")]
46    CODE503(AnthropicErrorBody),
47    /// # Missed cases for error codes, includes Status Code and Error Body as a string. These can also represent internal logic errors.
48    #[error("Undefined Error. This should not happen, if this is a missed error please report it: https://github.com/JackMatthewRimmer/rust-rag-toolchain: status code = {0}, error = {1}")]
49    Undefined(u16, String),
50    /// # Carries underlying error that may have occurred during sending the request
51    #[error("Error sending request: {0}")]
52    ErrorSendingRequest(String),
53    /// # Carries underlying error that may have occured when trying to get the response body
54    #[error("Error getting response body: {0}")]
55    ErrorGettingResponseBody(String),
56    // # Carries underlying error and the status code
57    #[error("Error deserializining response body: status code = {0}, error = {1}")]
58    ErrorDeserializingResponseBody(u16, String),
59}