-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #259: Improve
Bad Request
error message for uploading
ce50f26 feat: [#257] improve bad request error message for uploading (Jose Celano) Pull request description: Add a custom error for each case so that the Bad Request response constains a descriptive error message. Top commit has no ACKs. Tree-SHA512: 70659460ad69a694da21e66416bb51d2a42b743f6ed16a0d15fb460197b98b71921de740b707a50e73ce9e0d7239f22e92cd252b97a87af0bf3975d4043dd757
- Loading branch information
Showing
8 changed files
with
154 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use axum::response::{IntoResponse, Response}; | ||
use derive_more::{Display, Error}; | ||
use hyper::StatusCode; | ||
|
||
use crate::web::api::v1::responses::{json_error_response, ErrorResponseData}; | ||
|
||
#[derive(Debug, Display, PartialEq, Eq, Error)] | ||
pub enum Request { | ||
#[display(fmt = "torrent title bytes are nota valid UTF8 string.")] | ||
TitleIsNotValidUtf8, | ||
|
||
#[display(fmt = "torrent description bytes are nota valid UTF8 string.")] | ||
DescriptionIsNotValidUtf8, | ||
|
||
#[display(fmt = "torrent category bytes are nota valid UTF8 string.")] | ||
CategoryIsNotValidUtf8, | ||
|
||
#[display(fmt = "torrent tags arrays bytes are nota valid UTF8 string array.")] | ||
TagsArrayIsNotValidUtf8, | ||
|
||
#[display(fmt = "torrent tags string is not a valid JSON.")] | ||
TagsArrayIsNotValidJson, | ||
|
||
#[display(fmt = "upload torrent request header `content-type` should be `application/x-bittorrent`.")] | ||
InvalidFileType, | ||
|
||
#[display(fmt = "cannot write uploaded torrent bytes (binary file) into memory.")] | ||
CannotWriteChunkFromUploadedBinary, | ||
|
||
#[display(fmt = "cannot read a chunk of bytes from the uploaded torrent file. Review the request body size limit.")] | ||
CannotReadChunkFromUploadedBinary, | ||
|
||
#[display(fmt = "provided path param for Info-hash is not valid.")] | ||
InvalidInfoHashParam, | ||
} | ||
|
||
impl IntoResponse for Request { | ||
fn into_response(self) -> Response { | ||
json_error_response( | ||
http_status_code_for_handler_error(&self), | ||
&ErrorResponseData { error: self.to_string() }, | ||
) | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn http_status_code_for_handler_error(error: &Request) -> StatusCode { | ||
#[allow(clippy::match_same_arms)] | ||
match error { | ||
Request::TitleIsNotValidUtf8 => StatusCode::BAD_REQUEST, | ||
Request::DescriptionIsNotValidUtf8 => StatusCode::BAD_REQUEST, | ||
Request::CategoryIsNotValidUtf8 => StatusCode::BAD_REQUEST, | ||
Request::TagsArrayIsNotValidUtf8 => StatusCode::BAD_REQUEST, | ||
Request::TagsArrayIsNotValidJson => StatusCode::BAD_REQUEST, | ||
Request::InvalidFileType => StatusCode::BAD_REQUEST, | ||
Request::InvalidInfoHashParam => StatusCode::BAD_REQUEST, | ||
// Internal errors processing the request | ||
Request::CannotWriteChunkFromUploadedBinary => StatusCode::INTERNAL_SERVER_ERROR, | ||
Request::CannotReadChunkFromUploadedBinary => StatusCode::INTERNAL_SERVER_ERROR, | ||
} | ||
} |
Oops, something went wrong.