|
@@ -1,5 +1,6 @@
|
1
|
1
|
use diesel::result::Error;
|
2
|
2
|
use rocket::http::Status;
|
|
3
|
+use rocket::request::Request;
|
3
|
4
|
use rocket::response::status;
|
4
|
5
|
use rocket_contrib::json::Json;
|
5
|
6
|
use rocket_contrib::templates::Template;
|
|
@@ -53,3 +54,21 @@ pub fn create_snippet<'a>(
|
53
|
54
|
.map(|snippet| status::Created(String::from(""), Some(Json(snippet))))
|
54
|
55
|
.map_err(error_response)
|
55
|
56
|
}
|
|
57
|
+
|
|
58
|
+#[catch(400)]
|
|
59
|
+pub fn bad_request(req: &Request) -> String {
|
|
60
|
+ req.headers().get_one("content-length").map_or(
|
|
61
|
+ String::from("{\"message\": \"Bad request\"}"),
|
|
62
|
+ |length| {
|
|
63
|
+ if length
|
|
64
|
+ .parse::<i32>()
|
|
65
|
+ .expect("Content length is non-numeric")
|
|
66
|
+ > 1_000_000
|
|
67
|
+ {
|
|
68
|
+ String::from("{\"message\": \"Snippet must be under 1mb\"}")
|
|
69
|
+ } else {
|
|
70
|
+ String::from("{\"message\": \"Bad request\"}")
|
|
71
|
+ }
|
|
72
|
+ },
|
|
73
|
+ )
|
|
74
|
+}
|