async_dashscope/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::fmt::Display;

use serde::Deserialize;



#[derive(Debug, thiserror::Error)]
pub enum DashScopeError {
    #[error("http error: {0}")]
    Reqwest(#[from] reqwest::Error),
    #[error("failed to deserialize api response: {0}")]
    JSONDeserialize(serde_json::Error),
    #[error("{0}")]
    ApiError(ApiError),
    #[error("invalid argument:{0}")]
    InvalidArgument(String),
    #[error("stream error:{0}")]
    StreamError(String)
}

#[derive(Debug, Deserialize, Clone)]
pub struct ApiError {
    pub message: String,
    pub request_id: Option<String>,
    pub code: Option<String>,
}

impl Display for ApiError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut parts = Vec::new();
        if let Some(code) = &self.code {
            parts.push(format!("code: {}", code));
        }
        if let Some(request_id) = &self.request_id {
            parts.push(format!("request_id: {}", request_id));
        }
        write!(f, "{}", parts.join(" "))
    }
}

impl From<crate::operation::common::ParametersBuilderError> for DashScopeError {
    fn from(error: crate::operation::common::ParametersBuilderError) -> Self {
        DashScopeError::InvalidArgument(error.to_string())
    }
}


pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> DashScopeError {
    tracing::error!(
        "failed deserialization of: {}",
        String::from_utf8_lossy(bytes)
    );
    DashScopeError::JSONDeserialize(e)
}

pub type Result<T> = std::result::Result<T, DashScopeError>;