Skip to content

Commit f03b7f3

Browse files
committed
Rename error -> response
1 parent b8df383 commit f03b7f3

File tree

7 files changed

+57
-49
lines changed

7 files changed

+57
-49
lines changed

src/error.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/handler/fs.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tokio::fs;
66
use tokio::io::ErrorKind;
77
use urlencoding::decode;
88

9-
use crate::{config, error};
9+
use crate::{config, response};
1010

1111
#[derive(Debug)]
1212
pub struct FileHandler {
@@ -30,7 +30,7 @@ impl FileHandler {
3030
if request.method() != http::Method::GET {
3131
return Err((
3232
request,
33-
error::from_status(http::StatusCode::METHOD_NOT_ALLOWED),
33+
response::from_status(http::StatusCode::METHOD_NOT_ALLOWED),
3434
));
3535
}
3636

@@ -57,7 +57,7 @@ impl DirHandler {
5757
if request.method() != http::Method::GET {
5858
return Err((
5959
request,
60-
error::from_status(http::StatusCode::METHOD_NOT_ALLOWED),
60+
response::from_status(http::StatusCode::METHOD_NOT_ALLOWED),
6161
));
6262
}
6363

@@ -71,7 +71,7 @@ impl DirHandler {
7171
Ok(path) => path,
7272
Err(err) => {
7373
log::info!("Invalid path `{}`: {}", path, err);
74-
return Ok(error::from_status(http::StatusCode::BAD_REQUEST));
74+
return Ok(response::from_status(http::StatusCode::BAD_REQUEST));
7575
}
7676
};
7777

@@ -81,7 +81,7 @@ impl DirHandler {
8181
path.extend(components);
8282
Ok(file_response(&path).await)
8383
}
84-
None => Ok(error::from_status(http::StatusCode::NOT_FOUND)),
84+
None => Ok(response::from_status(http::StatusCode::NOT_FOUND)),
8585
}
8686
}
8787
}
@@ -113,26 +113,26 @@ async fn file_response(path: &Path) -> http::Response<Body> {
113113
Ok(file) => file,
114114
Err(err) if err.kind() == ErrorKind::NotFound => {
115115
log::info!("File not found: `{}`", path.display());
116-
return error::from_status(http::StatusCode::NOT_FOUND);
116+
return response::from_status(http::StatusCode::NOT_FOUND);
117117
}
118118
#[cfg(windows)]
119119
Err(err) if err.raw_os_error() == Some(123) => {
120120
log::info!("Invalid file name: `{}`", path.display());
121-
return error::from_status(http::StatusCode::NOT_FOUND);
121+
return response::from_status(http::StatusCode::NOT_FOUND);
122122
}
123123
#[cfg(windows)]
124124
Err(err) if err.raw_os_error() == Some(5) && path.is_dir() => {
125125
log::info!("Path is a directory: `{}`", path.display());
126-
return error::from_status(http::StatusCode::NOT_FOUND);
126+
return response::from_status(http::StatusCode::NOT_FOUND);
127127
}
128128
#[cfg(unix)]
129129
Err(err) if err.raw_os_error() == Some(21) => {
130130
log::info!("Path is a directory: `{}`", path.display());
131-
return error::from_status(http::StatusCode::NOT_FOUND);
131+
return response::from_status(http::StatusCode::NOT_FOUND);
132132
}
133133
Err(err) => {
134134
log::error!("Error opening file: {}", err);
135-
return error::from_status(http::StatusCode::INTERNAL_SERVER_ERROR);
135+
return response::from_status(http::StatusCode::INTERNAL_SERVER_ERROR);
136136
}
137137
};
138138

src/handler/json.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use headers::{ContentType, HeaderMapExt};
77
use hyper::body::{self, Body};
88
use json_patch::{Patch, PatchError};
99
use mime::Mime;
10-
use serde::{de::DeserializeOwned, ser::Serialize};
10+
use serde::de::DeserializeOwned;
1111
use tokio::fs::{self, File};
1212
use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
1313
use tokio::sync::{Notify, RwLock};
1414
use urlencoding::decode;
1515

16-
use crate::{config, error};
16+
use crate::{config, response};
1717

1818
#[derive(Debug)]
1919
pub struct JsonHandler {
@@ -77,7 +77,7 @@ impl JsonHandler {
7777
Ok(path) => path,
7878
Err(err) => {
7979
log::info!("Invalid path `{}`: {}", path, err);
80-
return Ok(error::from_status(http::StatusCode::BAD_REQUEST));
80+
return Ok(response::from_status(http::StatusCode::BAD_REQUEST));
8181
}
8282
};
8383

@@ -86,18 +86,18 @@ impl JsonHandler {
8686
&http::Method::PATCH => Ok(self.handle_patch(request, &path).await),
8787
_ => Err((
8888
request,
89-
error::from_status(http::StatusCode::METHOD_NOT_ALLOWED),
89+
response::from_status(http::StatusCode::METHOD_NOT_ALLOWED),
9090
)),
9191
}
9292
}
9393

9494
pub async fn handle_get(&self, _: http::Request<Body>, path: &str) -> http::Response<Body> {
9595
let value = self.state.value.read().await;
9696
match value.pointer(path) {
97-
Some(subvalue) => json_response(subvalue),
97+
Some(subvalue) => response::json(subvalue),
9898
None => {
9999
log::info!("Pointer `{}` did not match JSON", path);
100-
return error::from_status(http::StatusCode::NOT_FOUND);
100+
return response::from_status(http::StatusCode::NOT_FOUND);
101101
}
102102
}
103103
}
@@ -118,19 +118,21 @@ impl JsonHandler {
118118
Some(subvalue) => subvalue,
119119
None => {
120120
log::info!("Pointer `{}` did not match JSON", path);
121-
return error::from_status(http::StatusCode::NOT_FOUND);
121+
return response::from_status(http::StatusCode::NOT_FOUND);
122122
}
123123
};
124124

125125
if let Err(err) = json_patch::patch(subvalue, &patch) {
126126
log::info!("Failed to apply patch: {}", err);
127127
return match err {
128-
PatchError::TestFailed => error::from_status(http::StatusCode::CONFLICT),
129-
PatchError::InvalidPointer => error::from_status(http::StatusCode::NOT_FOUND),
128+
PatchError::TestFailed => response::from_status(http::StatusCode::CONFLICT),
129+
PatchError::InvalidPointer => {
130+
response::from_status(http::StatusCode::NOT_FOUND)
131+
}
130132
};
131133
}
132134

133-
json_response(subvalue)
135+
response::json(subvalue)
134136
};
135137

136138
self.state.dirty.notify();
@@ -182,12 +184,14 @@ async fn json_request<T: DeserializeOwned>(
182184
match request.headers().typed_try_get::<ContentType>() {
183185
Err(err) => {
184186
log::info!("Error parsing content-type header: {}", err);
185-
return Err(error::from_status(http::StatusCode::BAD_REQUEST));
187+
return Err(response::from_status(http::StatusCode::BAD_REQUEST));
186188
}
187189
Ok(Some(content_type)) => {
188190
let mime: Mime = content_type.into();
189191
if mime.subtype() != mime::JSON && mime.suffix() != Some(mime::JSON) {
190-
return Err(error::from_status(http::StatusCode::UNSUPPORTED_MEDIA_TYPE));
192+
return Err(response::from_status(
193+
http::StatusCode::UNSUPPORTED_MEDIA_TYPE,
194+
));
191195
}
192196
}
193197
Ok(None) => (),
@@ -197,22 +201,17 @@ async fn json_request<T: DeserializeOwned>(
197201
Ok(buf) => buf,
198202
Err(err) => {
199203
log::error!("Error reading request body: {}", err);
200-
return Err(error::from_status(http::StatusCode::INTERNAL_SERVER_ERROR));
204+
return Err(response::from_status(
205+
http::StatusCode::INTERNAL_SERVER_ERROR,
206+
));
201207
}
202208
};
203209

204210
match serde_json::from_reader(buf.reader()) {
205211
Ok(value) => Ok(value),
206212
Err(err) => {
207213
log::info!("Error deserializing request body: {}", err);
208-
Err(error::from_status(http::StatusCode::BAD_REQUEST))
214+
Err(response::from_status(http::StatusCode::BAD_REQUEST))
209215
}
210216
}
211217
}
212-
213-
fn json_response<T: Serialize>(value: &T) -> http::Response<Body> {
214-
let body = serde_json::to_string(value).expect("writing value to string should not fail");
215-
let mut response = http::Response::new(body.into());
216-
response.headers_mut().typed_insert(ContentType::json());
217-
response
218-
}

src/handler/proxy.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hyper::Body;
66
use hyper_rustls::HttpsConnector;
77
use once_cell::sync::Lazy;
88

9-
use crate::{config, error};
9+
use crate::{config, response};
1010

1111
#[derive(Debug)]
1212
pub struct ProxyHandler {
@@ -34,7 +34,7 @@ impl ProxyHandler {
3434
Ok(uri) => uri,
3535
Err(err) => {
3636
log::info!("path `{}` produced invalid uri: {}", path, err);
37-
return Ok(error::from_status(http::StatusCode::NOT_FOUND));
37+
return Ok(response::from_status(http::StatusCode::NOT_FOUND));
3838
}
3939
};
4040

@@ -52,12 +52,12 @@ impl ProxyHandler {
5252
log::debug!("Forwarding request to `{}`", request.uri());
5353

5454
match self.client.request(request).await {
55-
Ok(response) => {
56-
Ok(response)
57-
}
55+
Ok(response) => Ok(response),
5856
Err(err) => {
5957
log::error!("Error making request: {}", err);
60-
Ok(error::from_status(http::StatusCode::INTERNAL_SERVER_ERROR))
58+
Ok(response::from_status(
59+
http::StatusCode::INTERNAL_SERVER_ERROR,
60+
))
6161
}
6262
}
6363
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use structopt::StructOpt;
22

33
mod config;
4-
mod error;
54
mod handler;
5+
mod response;
66
mod route;
77
mod server;
88
mod tls;

src/response.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use headers::{ContentType, HeaderMapExt};
2+
use hyper::Body;
3+
use serde::Serialize;
4+
5+
pub fn from_status(status: http::StatusCode) -> http::Response<Body> {
6+
http::Response::builder()
7+
.status(status)
8+
.body(Body::empty())
9+
.unwrap()
10+
}
11+
12+
pub fn json<T: Serialize>(value: &T) -> http::Response<Body> {
13+
let body = serde_json::to_string(value).expect("writing value to string should not fail");
14+
let mut response = http::Response::new(body.into());
15+
response.headers_mut().typed_insert(ContentType::json());
16+
response
17+
}

src/route.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use regex::{Regex, RegexSet};
1515
use serde::de::{self, Deserialize, Deserializer};
1616

1717
use crate::config::Config;
18-
use crate::error;
1918
use crate::handler::Handler;
19+
use crate::response;
2020

2121
#[derive(Debug)]
2222
pub struct Router {
@@ -56,7 +56,7 @@ impl Router {
5656
self: Arc<Self>,
5757
mut request: http::Request<Body>,
5858
) -> http::Response<Body> {
59-
let mut response = error::from_status(http::StatusCode::NOT_FOUND);
59+
let mut response = response::from_status(http::StatusCode::NOT_FOUND);
6060

6161
let matches = self.regex_set.matches(request.uri().path());
6262
if matches.matched_any() {
@@ -87,7 +87,7 @@ impl Router {
8787
"Panic while handling request: {}",
8888
fmt_panic_payload(payload)
8989
);
90-
error::from_status(http::StatusCode::INTERNAL_SERVER_ERROR)
90+
response::from_status(http::StatusCode::INTERNAL_SERVER_ERROR)
9191
})
9292
}
9393

0 commit comments

Comments
 (0)