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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use serde_derive::{Deserialize, Serialize};

#[derive(Debug, Clone)]
pub enum LineError {
    ApiError(LineApiError),
    HttpError(LineHttpError),
    SystemError(LineSystemError),
}

impl LineError {
    pub fn status(&self) -> u16 {
        match self {
            LineError::ApiError(e) => e.status,
            LineError::HttpError(e) => e.status,
            LineError::SystemError(_) => 0,
        }
    }
    pub fn api_error(&self) -> Option<&LineApiErrorResponse> {
        match self {
            LineError::ApiError(e) => Some(&e.error),
            LineError::HttpError(_) => None,
            LineError::SystemError(_) => None,
        }
    }
}

impl From<LineApiError> for LineError {
    fn from(value: LineApiError) -> Self {
        LineError::ApiError(value)
    }
}

impl From<LineHttpError> for LineError {
    fn from(value: LineHttpError) -> Self {
        LineError::HttpError(value)
    }
}

impl From<LineSystemError> for LineError {
    fn from(value: LineSystemError) -> Self {
        LineError::SystemError(value)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LineApiError {
    pub status: u16,
    pub error: LineApiErrorResponse,
    pub warnings: Option<Vec<String>>,
    pub http_response_body: Option<String>,
}

/// https://developers.line.biz/ja/reference/messaging-api/#error-responses
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LineApiErrorResponse {
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Vec<LineApiErrorResponseDetail>>,
}

/// https://developers.line.biz/ja/reference/messaging-api/#error-responses
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LineApiErrorResponseDetail {
    pub message: String,
    pub property: String,
}

impl LineApiErrorResponse {
    pub fn debug_print(&self) {
        println!("{}", self.message);
        if let Some(details) = &self.details {
            for detail in details {
                println!(" {}: {}", detail.property, detail.message);
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct LineHttpError {
    pub status: u16,
    pub http_response_body: Option<String>,
}

impl LineHttpError {
    pub fn new(status: u16, http_response_body: String) -> LineHttpError {
        LineHttpError {
            status,
            http_response_body: Some(http_response_body),
        }
    }
}

#[derive(Debug, Clone)]
pub struct LineSystemError {
    pub message: Option<String>,
}

impl LineSystemError {
    pub fn new(message: String) -> LineSystemError {
        LineSystemError {
            message: Some(message),
        }
    }
}