From 7f4e97535b2e0413b8b9d23e2b06bf2a9ad83777 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Tue, 8 Apr 2025 18:12:46 +0200 Subject: [PATCH] Use serde_json::Error::is_data() to distinguish error kinds --- crates/handlers/src/compat/mod.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/crates/handlers/src/compat/mod.rs b/crates/handlers/src/compat/mod.rs index 5930cbd7e..fcb45d68c 100644 --- a/crates/handlers/src/compat/mod.rs +++ b/crates/handlers/src/compat/mod.rs @@ -53,11 +53,8 @@ pub enum MatrixJsonBodyRejection { #[error("Failed to read request body")] BytesRejection(#[from] BytesRejection), - #[error("Body is not a valid JSON document")] - NotJson(#[source] serde_json::Error), - - #[error("Request body has invalid parameters")] - BadJson(#[source] serde_json::Error), + #[error("Invalid JSON document")] + Json(#[from] serde_json::Error), } impl IntoResponse for MatrixJsonBodyRejection { @@ -92,15 +89,15 @@ impl IntoResponse for MatrixJsonBodyRejection { status: StatusCode::BAD_REQUEST, }, - Self::NotJson(_) => MatrixError { - errcode: "M_NOT_JSON", - error: "Body is not a valid JSON document", + Self::Json(err) if err.is_data() => MatrixError { + errcode: "M_BAD_JSON", + error: "JSON fields are not valid", status: StatusCode::BAD_REQUEST, }, - Self::BadJson(_) => MatrixError { - errcode: "M_BAD_JSON", - error: "JSON fields are not valid", + Self::Json(_) => MatrixError { + errcode: "M_NOT_JSON", + error: "Body is not a valid JSON document", status: StatusCode::BAD_REQUEST, }, }; @@ -138,11 +135,8 @@ where let bytes = Bytes::from_request(req, state).await?; - // We first parse it as a JSON value so that we can distinguish between - // invalid JSON documents and invalid requests - let value: serde_json::Value = - serde_json::from_slice(&bytes).map_err(MatrixJsonBodyRejection::NotJson)?; - let value = serde_json::from_value(value).map_err(MatrixJsonBodyRejection::BadJson)?; + let value: T = serde_json::from_slice(&bytes)?; + Ok(Self(value)) } }