async_anthropic/
errors.rs1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum AnthropicError {
6 #[error("network error: {0}")]
7 NetworkError(#[from] reqwest::Error),
8
9 #[error("malformed request: {0}")]
10 BadRequest(String),
11
12 #[error("api error: {0}")]
13 ApiError(String),
14
15 #[error("unauthorized; check your API key")]
16 Unauthorized,
17
18 #[error("failed to deserialize response: {0}")]
19 DeserializationError(#[from] serde_json::Error),
20
21 #[error("unknown error: {0}")]
22 Unknown(String),
23
24 #[error("unexpected error occurred")]
25 UnexpectedError,
26
27 #[error("stream failed: {0}")]
28 StreamError(StreamError),
29}
30
31impl From<backoff::Error<AnthropicError>> for AnthropicError {
32 fn from(err: backoff::Error<AnthropicError>) -> Self {
33 match err {
34 backoff::Error::Permanent(err) => err,
35 backoff::Error::Transient { .. } => AnthropicError::UnexpectedError,
36 }
37 }
38}
39
40#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Serialize)]
41pub struct StreamError {
42 #[serde(rename = "type")]
43 pub error_type: String,
44 pub message: String,
45}
46
47impl std::fmt::Display for StreamError {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 f.write_fmt(format_args!(
50 "Error ({}): {}",
51 self.error_type, self.message
52 ))
53 }
54}
55
56pub(crate) fn map_deserialization_error(e: serde_json::Error, _bytes: &[u8]) -> AnthropicError {
57 AnthropicError::DeserializationError(e)
58}