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
use serde::Deserialize;
use std::fmt;

#[derive(Debug, Deserialize, Clone)]
pub struct VkError {
    pub error: Error,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Error {
    #[serde(alias = "error_code")]
    pub code: i32,
    #[serde(alias = "error_msg")]
    pub message: String,
    pub request_params: Vec<RequestParam>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct RequestParam {
    pub key: String,
    pub value: String,
}

#[derive(Debug)]
pub enum VkApiError {
    ReqwestError(reqwest::Error),
    SerdeJsonError(serde_json::Error),
    VkError(VkError),
    InternalError(String),
}

impl From<reqwest::Error> for VkApiError {
    fn from(error: reqwest::Error) -> Self {
        VkApiError::ReqwestError(error)
    }
}

impl From<serde_json::Error> for VkApiError {
    fn from(error: serde_json::Error) -> Self {
        VkApiError::SerdeJsonError(error)
    }
}

impl fmt::Display for VkApiError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            VkApiError::ReqwestError(error) => write!(f, "Reqwest Error: {}", error),
            VkApiError::SerdeJsonError(error) => write!(f, "Serde JSON Error: {}", error),
            VkApiError::VkError(response) => write!(f, "VK Error: {:?}", response),
            VkApiError::InternalError(response) => write!(f, "Internal Error: {:?}", response),
        }
    }
}

impl std::error::Error for VkApiError {}