Emit error on non-UTF8 characters in path segments
This commit is contained in:
parent
e5e89b8929
commit
c346b4f681
@ -151,9 +151,16 @@ impl ToTokens for Api {
|
||||
use ruma_api::error::RequestDeserializationError;
|
||||
|
||||
let segment = path_segments.get(#i).unwrap().as_bytes();
|
||||
let decoded =
|
||||
ruma_api::exports::percent_encoding::percent_decode(segment)
|
||||
.decode_utf8_lossy();
|
||||
let decoded = match ruma_api::exports::percent_encoding::percent_decode(
|
||||
segment
|
||||
).decode_utf8() {
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
return Err(
|
||||
RequestDeserializationError::new(err, request).into()
|
||||
);
|
||||
}
|
||||
};
|
||||
match std::convert::TryFrom::try_from(decoded.deref()) {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
|
@ -191,6 +191,7 @@ enum SerializationError {
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug)]
|
||||
pub enum DeserializationError {
|
||||
Utf8(std::str::Utf8Error),
|
||||
Json(serde_json::Error),
|
||||
Query(serde_urlencoded::de::Error),
|
||||
Ident(ruma_identifiers::Error),
|
||||
@ -202,6 +203,7 @@ pub enum DeserializationError {
|
||||
impl Display for DeserializationError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
DeserializationError::Utf8(err) => Display::fmt(err, f),
|
||||
DeserializationError::Json(err) => Display::fmt(err, f),
|
||||
DeserializationError::Query(err) => Display::fmt(err, f),
|
||||
DeserializationError::Ident(err) => Display::fmt(err, f),
|
||||
@ -210,6 +212,13 @@ impl Display for DeserializationError {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<std::str::Utf8Error> for DeserializationError {
|
||||
fn from(err: std::str::Utf8Error) -> Self {
|
||||
Self::Utf8(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<serde_json::Error> for DeserializationError {
|
||||
fn from(err: serde_json::Error) -> Self {
|
||||
|
@ -352,7 +352,13 @@ mod tests {
|
||||
room_id: request_body.room_id,
|
||||
room_alias: {
|
||||
let segment = path_segments.get(5).unwrap().as_bytes();
|
||||
let decoded = percent_encoding::percent_decode(segment).decode_utf8_lossy();
|
||||
let decoded = match percent_encoding::percent_decode(segment).decode_utf8()
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(err) => {
|
||||
return Err(RequestDeserializationError::new(err, request).into())
|
||||
}
|
||||
};
|
||||
match serde_json::from_str(decoded.deref()) {
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user