Remove runtime panic caused by header <-> string conversion
This commit is contained in:
parent
4c01fee5c5
commit
b68b63ab8d
@ -34,8 +34,7 @@ impl Request {
|
||||
quote! {
|
||||
headers.append(
|
||||
ruma_api::exports::http::header::#header_name,
|
||||
ruma_api::exports::http::header::HeaderValue::from_str(request.#field_name.as_ref())
|
||||
.expect("failed to convert value into HeaderValue"),
|
||||
ruma_api::exports::http::header::HeaderValue::from_str(request.#field_name.as_ref())?,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
@ -56,10 +56,12 @@ impl Response {
|
||||
}
|
||||
ResponseField::Header(_, header_name) => {
|
||||
quote_spanned! {span=>
|
||||
#field_name: headers.remove(ruma_api::exports::http::header::#header_name)
|
||||
#field_name: ruma_api::try_deserialize!(
|
||||
response,
|
||||
headers.remove(ruma_api::exports::http::header::#header_name)
|
||||
.expect("response missing expected header")
|
||||
.to_str()
|
||||
.expect("failed to convert HeaderValue to str")
|
||||
)
|
||||
.to_owned()
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,13 @@ impl From<ruma_serde::urlencoded::ser::Error> for IntoHttpError {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<http::header::InvalidHeaderValue> for IntoHttpError {
|
||||
fn from(err: http::header::InvalidHeaderValue) -> Self {
|
||||
Self(SerializationError::Header(err))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for IntoHttpError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match &self.0 {
|
||||
@ -42,6 +49,7 @@ impl Display for IntoHttpError {
|
||||
SerializationError::Query(err) => {
|
||||
write!(f, "Query parameter serialization failed: {}", err)
|
||||
}
|
||||
SerializationError::Header(err) => write!(f, "Header serialization failed: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -196,6 +204,7 @@ impl<E: std::error::Error> std::error::Error for ServerError<E> {}
|
||||
enum SerializationError {
|
||||
Json(serde_json::Error),
|
||||
Query(ruma_serde::urlencoded::ser::Error),
|
||||
Header(http::header::InvalidHeaderValue),
|
||||
}
|
||||
|
||||
/// This type is public so it is accessible from `ruma_api!` generated code.
|
||||
@ -210,6 +219,7 @@ pub enum DeserializationError {
|
||||
// String <> Enum conversion failed. This can currently only happen in path
|
||||
// segment deserialization
|
||||
Strum(strum::ParseError),
|
||||
Header(http::header::ToStrError),
|
||||
}
|
||||
|
||||
impl Display for DeserializationError {
|
||||
@ -220,10 +230,18 @@ impl Display for DeserializationError {
|
||||
DeserializationError::Query(err) => Display::fmt(err, f),
|
||||
DeserializationError::Ident(err) => Display::fmt(err, f),
|
||||
DeserializationError::Strum(err) => Display::fmt(err, f),
|
||||
DeserializationError::Header(err) => Display::fmt(err, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<http::header::ToStrError> for DeserializationError {
|
||||
fn from(err: http::header::ToStrError) -> Self {
|
||||
Self::Header(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl From<std::str::Utf8Error> for DeserializationError {
|
||||
fn from(err: std::str::Utf8Error) -> Self {
|
||||
|
Loading…
x
Reference in New Issue
Block a user