Skip to main content

line_bot_sdk_rust/line_module/apis/
mod.rs

1/*
2* Copyright (C) 2016 LINE Corp.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8*     http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17use std::fmt;
18use std::fmt::Debug;
19
20use hyper;
21use hyper::http;
22use hyper_util::client::legacy::connect::Connect;
23use serde_json;
24
25#[derive(Debug)]
26pub enum Error {
27    Api(ApiError),
28    Header(http::header::InvalidHeaderValue),
29    Http(http::Error),
30    Hyper(hyper::Error),
31    HyperClient(hyper_util::client::legacy::Error),
32    Serde(serde_json::Error),
33    Timeout,
34    UriError(http::uri::InvalidUri),
35}
36
37pub struct ApiError {
38    pub code: hyper::StatusCode,
39    pub body: hyper::body::Incoming,
40}
41
42impl Debug for ApiError {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        f.debug_struct("ApiError")
45            .field("code", &self.code)
46            .field("body", &"hyper::body::Incoming")
47            .finish()
48    }
49}
50
51impl From<(hyper::StatusCode, hyper::body::Incoming)> for Error {
52    fn from(e: (hyper::StatusCode, hyper::body::Incoming)) -> Self {
53        Error::Api(ApiError {
54            code: e.0,
55            body: e.1,
56        })
57    }
58}
59
60impl fmt::Display for Error {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            Error::Api(e) => write!(f, "API error (status {})", e.code),
64            Error::Header(e) => write!(f, "invalid header: {}", e),
65            Error::Http(e) => write!(f, "HTTP error: {}", e),
66            Error::Hyper(e) => write!(f, "hyper error: {}", e),
67            Error::HyperClient(e) => write!(f, "hyper client error: {}", e),
68            Error::Serde(e) => write!(f, "serde error: {}", e),
69            Error::Timeout => write!(f, "request timed out"),
70            Error::UriError(e) => write!(f, "URI error: {}", e),
71        }
72    }
73}
74
75impl std::error::Error for Error {
76    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
77        match self {
78            Error::Header(e) => Some(e),
79            Error::Http(e) => Some(e),
80            Error::Hyper(e) => Some(e),
81            Error::HyperClient(e) => Some(e),
82            Error::Serde(e) => Some(e),
83            Error::UriError(e) => Some(e),
84            _ => None,
85        }
86    }
87}
88
89impl From<http::Error> for Error {
90    fn from(e: http::Error) -> Self {
91        Error::Http(e)
92    }
93}
94
95impl From<hyper_util::client::legacy::Error> for Error {
96    fn from(e: hyper_util::client::legacy::Error) -> Self {
97        Error::HyperClient(e)
98    }
99}
100
101impl From<hyper::Error> for Error {
102    fn from(e: hyper::Error) -> Self {
103        Error::Hyper(e)
104    }
105}
106
107impl From<serde_json::Error> for Error {
108    fn from(e: serde_json::Error) -> Self {
109        Error::Serde(e)
110    }
111}
112
113mod line_module_api;
114mod request;
115pub use self::line_module_api::{LineModuleApi, LineModuleApiClient};
116pub mod client;
117pub mod configuration;