async_dashscope/
error.rs

1use std::fmt::Display;
2
3use serde::Deserialize;
4
5#[derive(Debug, thiserror::Error)]
6pub enum DashScopeError {
7    #[error("http error: {0}")]
8    Reqwest(#[from] reqwest::Error),
9    #[error("failed to deserialize api response: {0}")]
10    JSONDeserialize(serde_json::Error),
11    #[error("{0}")]
12    ApiError(ApiError),
13    #[error("invalid argument:{0}")]
14    InvalidArgument(String),
15    #[error("stream error:{0}")]
16    StreamError(String),
17}
18
19#[derive(Debug, Deserialize, Clone)]
20pub struct ApiError {
21    pub message: String,
22    pub request_id: Option<String>,
23    pub code: Option<String>,
24}
25
26impl Display for ApiError {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        let mut parts = Vec::new();
29        if let Some(code) = &self.code {
30            parts.push(format!("code: {}", code));
31        }
32        if let Some(request_id) = &self.request_id {
33            parts.push(format!("request_id: {}", request_id));
34        }
35        write!(f, "{}", parts.join(" "))
36    }
37}
38
39impl From<crate::operation::common::ParametersBuilderError> for DashScopeError {
40    fn from(error: crate::operation::common::ParametersBuilderError) -> Self {
41        DashScopeError::InvalidArgument(error.to_string())
42    }
43}
44
45pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> DashScopeError {
46    tracing::error!(
47        "failed deserialization of: {}",
48        String::from_utf8_lossy(bytes)
49    );
50    DashScopeError::JSONDeserialize(e)
51}
52
53pub type Result<T> = std::result::Result<T, DashScopeError>;