1mod api;
4mod core;
5mod server;
6
7pub use api::ApiError;
8pub use core::CoreError;
9pub use server::ServerError;
10
11type BoxError = Box<dyn std::error::Error + 'static + Send + Sync>;
13
14#[derive(thiserror::Error, Debug)]
16pub enum Error {
17 #[error("error in API Client: {0}")]
19 Api(#[from] ApiError),
20
21 #[error("error in commonly process: {0}")]
23 Core(#[from] CoreError),
24
25 #[error("error about server: {0}")]
27 Server(#[from] ServerError),
28
29 #[error("{0}")]
31 RequiredValueIsMissing(String),
32
33 #[error("failed to deserialize: {0}")]
35 DeserializeJson(serde_json::Error),
36
37 #[error("stacked error: message = {message}, stack = {stack:?}")]
39 Stacked {
40 message: &'static str,
42
43 stack: Option<Box<Error>>,
45 },
46}
47
48impl Error {
49 pub fn required_value_is_missing(values: &[&str]) -> Self {
51 if values.is_empty() {
52 Self::RequiredValueIsMissing("Some required value is missing.".into())
53 } else if values.len() == 1 {
54 Self::RequiredValueIsMissing(format!("`{}` is required.", values.first().unwrap()))
55 } else {
56 Self::RequiredValueIsMissing(format!("One of `{}` is required.", values.join("` or `")))
57 }
58 }
59
60 pub fn stack(self, message: &'static str) -> Self {
62 Self::Stacked {
63 message,
64 stack: Some(Box::new(self)),
65 }
66 }
67
68 pub fn failed_to_request_by_http(
70 inner: &'static str,
71 e: impl std::error::Error + 'static + Send + Sync,
72 ) -> Self {
73 Self::Api(ApiError::FailedToRequestByHttp(inner, Box::new(e)))
74 }
75
76 pub fn failed_to_read_json(
78 inner: &'static str,
79 e: impl std::error::Error + 'static + Send + Sync,
80 ) -> Self {
81 Self::Api(ApiError::FailedToReadJson(inner, Box::new(e)))
82 }
83
84 pub fn failed_to_read_stream(
86 inner: &'static str,
87 e: impl std::error::Error + 'static + Send + Sync,
88 ) -> Self {
89 Self::Api(ApiError::FailedToReadStream(inner, Box::new(e)))
90 }
91
92 pub fn failed_to_read_file_in_files_upload(
94 e: impl std::error::Error + 'static + Send + Sync,
95 ) -> Self {
96 Self::Api(ApiError::FailedToReadFileInFilesUpload(Box::new(e)))
97 }
98
99 pub fn failed_creating_mulipart_data(
101 e: impl std::error::Error + 'static + Send + Sync,
102 ) -> Self {
103 Self::Api(ApiError::FailedCreatingMulipartData(Box::new(e)))
104 }
105
106 pub fn parsing_number(
108 inner: &'static str,
109 e: impl std::error::Error + 'static + Send + Sync,
110 ) -> Self {
111 Self::Core(CoreError::ParsingNumber(inner, Box::new(e)))
112 }
113
114 pub fn failed_to_initialize_hmac(e: impl std::error::Error + 'static + Send + Sync) -> Self {
116 Self::Core(CoreError::FailedToInitializeHmac(Box::new(e)))
117 }
118
119 pub fn failed_to_bind_socket(e: impl std::error::Error + 'static + Send + Sync) -> Self {
121 Self::Server(ServerError::FailedToBindSocket(Box::new(e)))
122 }
123
124 pub fn failed_to_serve(e: impl std::error::Error + 'static + Send + Sync) -> Self {
126 Self::Server(ServerError::FailedToServe(Box::new(e)))
127 }
128}