diff --git a/src/api/router/args.rs b/src/api/router/args.rs index cc18438c0..ebf7d0237 100644 --- a/src/api/router/args.rs +++ b/src/api/router/args.rs @@ -11,7 +11,11 @@ use axum::{ use conduwuit::{Error, Result, err, error}; use ruma::{ CanonicalJsonObject, - api::{IncomingRequest, IncomingRequestExt, auth_scheme::AuthScheme}, + api::{ + IncomingRequest, IncomingRequestExt, + auth_scheme::AuthScheme, + error::{DeserializationError, FromHttpRequestError}, + }, }; use serde::Deserialize; @@ -114,8 +118,18 @@ where .await?; // Deserialize the body - let body = R::try_from_http_request(request, &borrowed_path) - .map_err(|e| err!(Request(BadJson(debug_warn!("{e}")))))?; + let body_is_empty = body.is_empty(); + let body = R::try_from_http_request(request, &borrowed_path).map_err(|e| match e { + | FromHttpRequestError::Deserialization(DeserializationError::Json(ref json_err)) + if body_is_empty || json_err.is_syntax() || json_err.is_eof() => + err!(Request(NotJson(debug_warn!("{e}")))), + | FromHttpRequestError::Deserialization(DeserializationError::Utf8(_)) => + err!(Request(NotJson(debug_warn!("{e}")))), + | FromHttpRequestError::Deserialization( + DeserializationError::Ident(_) | DeserializationError::Query(_), + ) => err!(Request(InvalidParam(debug_warn!("{e}")))), + | _ => err!(Request(BadJson(debug_warn!("{e}")))), + })?; Ok(Self { body, json_body, identity }) } diff --git a/src/core/error/mod.rs b/src/core/error/mod.rs index ad3040fff..ad7438c47 100644 --- a/src/core/error/mod.rs +++ b/src/core/error/mod.rs @@ -171,13 +171,14 @@ impl Error { /// Returns the Matrix error code / error kind #[inline] pub fn kind(&self) -> ErrorKind { - use ruma::api::error::ErrorKind::{Unknown, Unrecognized}; + use ruma::api::error::ErrorKind::{InvalidParam, Unknown, Unrecognized}; match self { | Self::Federation(_, error) | Self::Ruma(error) => response::ruma_error_kind(error).clone(), | BadRequest(kind, ..) | Self::Request(kind, ..) => kind.clone(), | Self::FeatureDisabled(..) => Unrecognized, + | Self::Path(_) | Self::Query(_) => InvalidParam, | _ => Unknown, } } @@ -194,6 +195,7 @@ impl Error { | Self::FeatureDisabled(..) => response::bad_request_code(&self.kind()), | Self::Reqwest(error) => error.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), | Self::Conflict(_) => StatusCode::CONFLICT, + | Self::Path(_) | Self::Query(_) => StatusCode::BAD_REQUEST, | Self::Io(error) => response::io_error_code(error.kind()), | Self::Uiaa(_) => StatusCode::UNAUTHORIZED, | _ => StatusCode::INTERNAL_SERVER_ERROR,