refactor(server): use more specific http status codes

This commit is contained in:
Helmut K. C. Tessarek 2024-03-08 17:53:45 -05:00
parent 4987cfe5e5
commit 08e1f23107
No known key found for this signature in database
GPG Key ID: BE0985349D44DD00
2 changed files with 5 additions and 14 deletions

View File

@ -98,14 +98,13 @@ impl Paste {
expiry_date: Option<u128>,
header_filename: Option<String>,
config: &Config,
) -> IoResult<String> {
) -> Result<String, Error> {
let file_type = infer::get(&self.data);
if let Some(file_type) = file_type {
for mime_type in &config.paste.mime_blacklist {
if mime_type == file_type.mime_type() {
return Err(IoError::new(
IoErrorKind::Other,
String::from("this file type is not permitted"),
return Err(error::ErrorUnsupportedMediaType(
"this file type is not permitted",
));
}
}
@ -179,10 +178,7 @@ impl Paste {
let file_path = util::glob_match_file(path.clone())
.map_err(|_| IoError::new(IoErrorKind::Other, String::from("path is not valid")))?;
if file_path.is_file() && file_path.exists() {
return Err(IoError::new(
IoErrorKind::AlreadyExists,
String::from("file already exists\n"),
));
return Err(error::ErrorConflict("file already exists\n"));
}
if let Some(timestamp) = expiry_date {
path.set_file_name(format!("{file_name}.{timestamp}"));

View File

@ -951,15 +951,10 @@ mod tests {
.to_request(),
)
.await;
assert_eq!(StatusCode::INTERNAL_SERVER_ERROR, response.status());
assert_eq!(StatusCode::CONFLICT, response.status());
assert_body(response.into_body(), "file already exists\n").await?;
fs::remove_file(header_filename)?;
let serve_request = TestRequest::get()
.uri(&format!("/{header_filename}"))
.to_request();
let response = test::call_service(&app, serve_request).await;
assert_eq!(StatusCode::NOT_FOUND, response.status());
Ok(())
}