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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::convert::From;
use std::io::Error as FSErrorInner;
use std::sync::Arc;
use actix_web::{
error::ResponseError,
http::{header, StatusCode},
HttpResponse, HttpResponseBuilder,
};
use config::ConfigError as ConfigErrorInner;
use derive_more::{Display, Error};
use git2::Error as GitError;
use serde::{Deserialize, Serialize};
use url::ParseError;
use crate::page::Page;
#[derive(Debug, Display, Error)]
pub struct FSError(#[display(fmt = "File System Error {}", _0)] pub FSErrorInner);
#[derive(Debug, Display, Error)]
pub struct ConfigError(#[display(fmt = "Configuration Error {}", _0)] pub ConfigErrorInner);
#[cfg(not(tarpaulin_include))]
impl PartialEq for FSError {
fn eq(&self, other: &Self) -> bool {
self.0.kind() == other.0.kind()
}
}
#[cfg(not(tarpaulin_include))]
impl PartialEq for ConfigError {
fn eq(&self, other: &Self) -> bool {
self.0.to_string().trim() == other.0.to_string().trim()
}
}
#[cfg(not(tarpaulin_include))]
impl From<FSErrorInner> for ServiceError {
fn from(e: FSErrorInner) -> Self {
Self::FSError(FSError(e))
}
}
#[cfg(not(tarpaulin_include))]
impl From<ConfigErrorInner> for ServiceError {
fn from(e: ConfigErrorInner) -> Self {
Self::ConfigError(ConfigError(e))
}
}
#[derive(Debug, Display, PartialEq, Error)]
#[cfg(not(tarpaulin_include))]
pub enum ServiceError {
#[display(fmt = "internal server error")]
InternalServerError,
#[display(fmt = "The value you entered for URL is not a URL")]
NotAUrl,
#[display(fmt = "URL too long, maximum length can't be greater then 2048 characters")]
URLTooLong,
#[display(fmt = "Website not found")]
WebsiteNotFound,
#[display(
fmt = "Path already used for another website. lhs: {:?} rhs: {:?}",
_0,
_1
)]
PathTaken(Arc<Page>, Arc<Page>),
#[display(
fmt = "Secret already used for another website. lhs: {:?} rhs: {:?}",
_0,
_1
)]
SecretTaken(Arc<Page>, Arc<Page>),
#[display(
fmt = "Repository URL already configured for another website deployment. lhs: {:?} rhs: {:?}",
_0,
_1
)]
DuplicateRepositoryURL(Arc<Page>, Arc<Page>),
#[display(fmt = "File System Error {}", _0)]
FSError(FSError),
#[display(fmt = "Unauthorized {}", _0)]
UnauthorizedOperation(#[error(not(source))] String),
#[display(fmt = "Bad request: {}", _0)]
BadRequest(#[error(not(source))] String),
#[display(fmt = "Configuration Error {}", _0)]
ConfigError(ConfigError),
#[display(fmt = "Git Error {}", _0)]
GitError(GitError),
#[display(fmt = "Branch {} not found", _0)]
BranchNotFound(#[error(not(source))] String),
}
impl From<ParseError> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(_: ParseError) -> ServiceError {
ServiceError::NotAUrl
}
}
impl From<GitError> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(e: GitError) -> ServiceError {
ServiceError::GitError(e)
}
}
#[cfg(not(tarpaulin_include))]
pub type ServiceResult<V> = std::result::Result<V, ServiceError>;
#[derive(Serialize, Deserialize, Debug)]
#[cfg(not(tarpaulin_include))]
pub struct ErrorToResponse {
pub error: String,
}
#[cfg(not(tarpaulin_include))]
impl ResponseError for ServiceError {
#[cfg(not(tarpaulin_include))]
fn error_response(&self) -> HttpResponse {
HttpResponseBuilder::new(self.status_code())
.append_header((header::CONTENT_TYPE, "application/json; charset=UTF-8"))
.body(
serde_json::to_string(&ErrorToResponse {
error: self.to_string(),
})
.unwrap(),
)
}
#[cfg(not(tarpaulin_include))]
fn status_code(&self) -> StatusCode {
match self {
ServiceError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::ConfigError(_) => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::NotAUrl => StatusCode::BAD_REQUEST,
ServiceError::URLTooLong => StatusCode::BAD_REQUEST,
ServiceError::WebsiteNotFound => StatusCode::NOT_FOUND,
ServiceError::PathTaken(_, _) => StatusCode::BAD_REQUEST,
ServiceError::DuplicateRepositoryURL(_, _) => StatusCode::BAD_REQUEST,
ServiceError::SecretTaken(_, _) => StatusCode::BAD_REQUEST,
ServiceError::FSError(_) => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::UnauthorizedOperation(_) => StatusCode::UNAUTHORIZED,
ServiceError::BadRequest(_) => StatusCode::BAD_REQUEST,
ServiceError::GitError(_) => StatusCode::BAD_REQUEST,
ServiceError::BranchNotFound(_) => StatusCode::CONFLICT,
}
}
}