style: error on invalid utf8 seq

This commit is contained in:
DarkCat09 2024-06-03 16:12:33 +04:00
parent 1c0437b820
commit 803acce7e9
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
2 changed files with 12 additions and 1 deletions

View file

@ -44,6 +44,9 @@ pub enum AppError {
min: Option<Decimal>,
},
#[display(fmt = "UTF8")]
Utf8Error(std::str::Utf8Error),
#[display(fmt = "IDK")]
UnknownBody {
json_err: Option<serde_json::Error>,
@ -63,6 +66,7 @@ impl web::error::WebResponseError for AppError {
AppError::RequestTooLarge => StatusCode::PAYLOAD_TOO_LARGE,
AppError::ServerRedisError(_) => StatusCode::INTERNAL_SERVER_ERROR,
AppError::ApiKeyInvalid { .. } => StatusCode::BAD_REQUEST,
AppError::Utf8Error(_) => StatusCode::BAD_REQUEST,
AppError::UnknownBody { .. } => StatusCode::BAD_REQUEST,
AppError::QSError(..) => StatusCode::BAD_REQUEST,
AppError::DeviceNotFound(..) => StatusCode::BAD_REQUEST,
@ -77,6 +81,7 @@ impl web::error::WebResponseError for AppError {
AppError::RequestTooLarge => "Request is too large",
AppError::ServerRedisError(_) => "Internal server error",
AppError::ApiKeyInvalid { .. } => "API Key invalid",
AppError::Utf8Error(_) => "Invalid UTF8 sequence",
AppError::UnknownBody { .. } => {
"Can't figure out where and in what encoding the main data is"
}

View file

@ -50,7 +50,13 @@ pub async fn device_handler<'a>(
Err(error) => json_error = Some(error),
},
"application/x-www-form-urlencoded" => {
match qs_parser::parse_nm_qs_format(&body.as_ref().to_str_lossy()).await {
let body = match std::str::from_utf8(body.as_ref()) {
Ok(body) => body,
Err(error) => {
return Err(AppError::Utf8Error(error));
}
};
match qs_parser::parse_nm_qs_format(body).await {
Ok(qs_body) => {
real_body = Some(NMJsonPacket {
devices: Vec::from([qs_body]),