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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
use failure::Error; #[allow(unused_imports)] use log::{debug, error, info, trace, warn}; use okapi::openapi3::Responses; use rocket::catch; use rocket::http::{ContentType, Status}; use rocket::request::Request; use rocket::response::{self, Responder, Response}; use rocket_contrib::json::Json; use rocket_okapi::gen::OpenApiGenerator; use rocket_okapi::response::OpenApiResponder; use rocket_okapi::Result as OpenApiResult; use serde::{Deserialize, Serialize}; fn add_204_error(responses: &mut Responses) { responses .responses .entry("204".to_owned()) .or_insert_with(|| { let mut response = okapi::openapi3::Response::default(); response.description = "\ # [204 No Content](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204)\n\ This response is given when you request an item using a filter and there is no \ item with the exact filter criteria.\n\n\ In most cases you requested an `id` that does not exists in the database. \ The body of this response will be empty.\ " .to_owned(); response.into() }); } fn add_304_error(responses: &mut Responses) { responses .responses .entry("304".to_owned()) .or_insert_with(|| { let mut response = okapi::openapi3::Response::default(); response.description = "\ # [304 Not Modified](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304)\n\ This response is given when the ETag of in the query parameters matches the \ generated response by the server. This can then be used to enable caching of \ responses.\n\n\ If the ETags do not match it will give you the full response (with a 200 OK). \ The response will give include the new ETag.\n\n\ [More info can be found here]\ (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match).\ " .to_owned(); response.into() }); } fn add_404_error(responses: &mut Responses) { responses.responses.entry("404".to_owned()) .or_insert_with(|| { let mut response = okapi::openapi3::Response::default(); response.description = "\ # [404 Not Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404)\n\ This response is given when you request a page that does not exists.\n\n\ **Note:** This is not exactly a response by this endpoint. But might returned when \ you wrongly input one or more of the path or query parameters. An example would be \ that the server expects and `int32` and you have given it \"100m\", which is a `string` \ because of the `m` character.\n\n\ So when you get this error and you expect a result. Check all the types of the parameters. \ ".to_owned(); response.into() }); } fn add_500_error(responses: &mut Responses) { responses .responses .entry("500".to_owned()) .or_insert_with(|| { let mut response = okapi::openapi3::Response::default(); response.description = format!( "\ # [500 Internal Server Error]\ (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500)\n\ This response is given when the server has an internal error that it could not \ recover from.\n\n\ If you get this response please report this as an [issue here]({}).\ ", df_st_core::git_issue::link_to_issue_template("bug") ); response.into() }); } #[derive(Serialize, Deserialize)] pub struct APIErrorResponse { error: APIError, } #[derive(Serialize, Deserialize)] pub struct APIError { pub message: String, pub issue_link: Option<String>, pub code: u16, } #[derive(Serialize, Deserialize)] pub struct APIMessageResponse { message: APIMessage, } #[derive(Serialize, Deserialize)] pub struct APIMessage { pub message: String, pub code: u16, } #[derive(Debug, Default)] pub struct APIErrorNoContent { pub err: Option<Error>, } impl APIErrorNoContent { pub fn new() -> Self { APIErrorNoContent::default() } } impl From<Error> for APIErrorNoContent { fn from(error: Error) -> Self { APIErrorNoContent { err: Some(error) } } } impl<'r> Responder<'r> for APIErrorNoContent { fn respond_to(self, _: &Request) -> response::Result<'r> { if let Some(error) = self.err { error!("{}", error); let error_response = APIErrorResponse { error: APIError { message: error.to_string(), issue_link: None, code: 500, }, }; return Response::build() .header(ContentType::JSON) .status(Status::InternalServerError) .sized_body(std::io::Cursor::new( serde_json::to_string(&error_response).unwrap(), )) .ok(); } Response::build().status(Status::NoContent).ok() } } impl OpenApiResponder<'_> for APIErrorNoContent { fn responses(_: &mut OpenApiGenerator) -> OpenApiResult<Responses> { let mut responses = Responses::default(); add_204_error(&mut responses); add_404_error(&mut responses); add_500_error(&mut responses); Ok(responses) } } #[derive(Debug, Default)] pub struct APIErrorNotModified { pub err: Option<Error>, } impl APIErrorNotModified { pub fn new() -> Self { APIErrorNotModified::default() } } impl From<Error> for APIErrorNotModified { fn from(error: Error) -> Self { APIErrorNotModified { err: Some(error) } } } impl<'r> Responder<'r> for APIErrorNotModified { fn respond_to(self, _: &Request) -> response::Result<'r> { if let Some(error) = self.err { error!("{}", error); let error_response = APIErrorResponse { error: APIError { message: error.to_string(), issue_link: None, code: 500, }, }; return Response::build() .header(ContentType::JSON) .status(Status::InternalServerError) .sized_body(std::io::Cursor::new( serde_json::to_string(&error_response).unwrap(), )) .ok(); } Response::build().status(Status::NotModified).ok() } } impl OpenApiResponder<'_> for APIErrorNotModified { fn responses(_: &mut OpenApiGenerator) -> OpenApiResult<Responses> { let mut responses = Responses::default(); add_304_error(&mut responses); add_404_error(&mut responses); add_500_error(&mut responses); Ok(responses) } } #[catch(500)] pub fn internal_error() -> Json<APIErrorResponse> { Json(APIErrorResponse { error: APIError { message: "I don't know what happened there... \ Did someone leave the caverns open?\n\ Please open a new issue for this: \ https://gitlab.com/df_storyteller/df-storyteller/issues/new" .to_owned(), issue_link: Some( "https://gitlab.com/df_storyteller/df-storyteller/issues/new".to_owned(), ), code: 500, }, }) } #[catch(404)] pub fn not_found(req: &Request) -> Json<APIErrorResponse> { Json(APIErrorResponse { error: APIError { message: format!("This requested URL `{}` was not found.", req.uri()), issue_link: None, code: 404, }, }) } #[catch(204)] pub fn no_content(req: &Request) -> Json<APIMessageResponse> { Json(APIMessageResponse { message: APIMessage { message: format!("No Content found here `{}`.", req.uri()), code: 204, }, }) } #[catch(304)] pub fn not_modified(_req: &Request) -> Json<APIMessageResponse> { Json(APIMessageResponse { message: APIMessage { message: "Content has not been modified.".to_owned(), code: 304, }, }) }