line_bot_messaging_api/
error.rs

1use serde_derive::{Deserialize, Serialize};
2
3#[derive(Debug, Clone)]
4pub enum LineError {
5    ApiError(LineApiError),
6    HttpError(LineHttpError),
7    SystemError(LineSystemError),
8}
9
10impl LineError {
11    pub fn status(&self) -> u16 {
12        match self {
13            LineError::ApiError(e) => e.status,
14            LineError::HttpError(e) => e.status,
15            LineError::SystemError(_) => 0,
16        }
17    }
18    pub fn api_error(&self) -> Option<&LineApiErrorResponse> {
19        match self {
20            LineError::ApiError(e) => Some(&e.error),
21            LineError::HttpError(_) => None,
22            LineError::SystemError(_) => None,
23        }
24    }
25}
26
27impl From<LineApiError> for LineError {
28    fn from(value: LineApiError) -> Self {
29        LineError::ApiError(value)
30    }
31}
32
33impl From<LineHttpError> for LineError {
34    fn from(value: LineHttpError) -> Self {
35        LineError::HttpError(value)
36    }
37}
38
39impl From<LineSystemError> for LineError {
40    fn from(value: LineSystemError) -> Self {
41        LineError::SystemError(value)
42    }
43}
44
45#[derive(Debug, Serialize, Deserialize, Clone)]
46pub struct LineApiError {
47    pub status: u16,
48    pub error: LineApiErrorResponse,
49    pub warnings: Option<Vec<String>>,
50    pub http_response_body: Option<String>,
51}
52
53/// https://developers.line.biz/ja/reference/messaging-api/#error-responses
54#[derive(Debug, Serialize, Deserialize, Clone)]
55pub struct LineApiErrorResponse {
56    pub message: String,
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub details: Option<Vec<LineApiErrorResponseDetail>>,
59}
60
61/// https://developers.line.biz/ja/reference/messaging-api/#error-responses
62#[derive(Debug, Serialize, Deserialize, Clone)]
63pub struct LineApiErrorResponseDetail {
64    pub message: String,
65    pub property: String,
66}
67
68impl LineApiErrorResponse {
69    pub fn debug_print(&self) {
70        println!("{}", self.message);
71        if let Some(details) = &self.details {
72            for detail in details {
73                println!(" {}: {}", detail.property, detail.message);
74            }
75        }
76    }
77}
78
79#[derive(Debug, Clone)]
80pub struct LineHttpError {
81    pub status: u16,
82    pub http_response_body: Option<String>,
83}
84
85impl LineHttpError {
86    pub fn new(status: u16, http_response_body: String) -> LineHttpError {
87        LineHttpError {
88            status,
89            http_response_body: Some(http_response_body),
90        }
91    }
92}
93
94#[derive(Debug, Clone)]
95pub struct LineSystemError {
96    pub message: Option<String>,
97}
98
99impl LineSystemError {
100    pub fn new(message: String) -> LineSystemError {
101        LineSystemError {
102            message: Some(message),
103        }
104    }
105}