Remove runtime panic caused by header <-> string conversion

This commit is contained in:
Devin Ragotzy 2020-07-28 17:01:36 -04:00
parent 4c01fee5c5
commit b68b63ab8d
3 changed files with 25 additions and 6 deletions

View File

@ -34,8 +34,7 @@ impl Request {
quote! { quote! {
headers.append( headers.append(
ruma_api::exports::http::header::#header_name, ruma_api::exports::http::header::#header_name,
ruma_api::exports::http::header::HeaderValue::from_str(request.#field_name.as_ref()) ruma_api::exports::http::header::HeaderValue::from_str(request.#field_name.as_ref())?,
.expect("failed to convert value into HeaderValue"),
); );
} }
}); });

View File

@ -56,10 +56,12 @@ impl Response {
} }
ResponseField::Header(_, header_name) => { ResponseField::Header(_, header_name) => {
quote_spanned! {span=> 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") .expect("response missing expected header")
.to_str() .to_str()
.expect("failed to convert HeaderValue to str") )
.to_owned() .to_owned()
} }
} }

View File

@ -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 { impl Display for IntoHttpError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.0 { match &self.0 {
@ -42,6 +49,7 @@ impl Display for IntoHttpError {
SerializationError::Query(err) => { SerializationError::Query(err) => {
write!(f, "Query parameter serialization failed: {}", 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 { enum SerializationError {
Json(serde_json::Error), Json(serde_json::Error),
Query(ruma_serde::urlencoded::ser::Error), Query(ruma_serde::urlencoded::ser::Error),
Header(http::header::InvalidHeaderValue),
} }
/// This type is public so it is accessible from `ruma_api!` generated code. /// 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 // String <> Enum conversion failed. This can currently only happen in path
// segment deserialization // segment deserialization
Strum(strum::ParseError), Strum(strum::ParseError),
Header(http::header::ToStrError),
} }
impl Display for DeserializationError { impl Display for DeserializationError {
@ -220,10 +230,18 @@ impl Display for DeserializationError {
DeserializationError::Query(err) => Display::fmt(err, f), DeserializationError::Query(err) => Display::fmt(err, f),
DeserializationError::Ident(err) => Display::fmt(err, f), DeserializationError::Ident(err) => Display::fmt(err, f),
DeserializationError::Strum(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)] #[doc(hidden)]
impl From<std::str::Utf8Error> for DeserializationError { impl From<std::str::Utf8Error> for DeserializationError {
fn from(err: std::str::Utf8Error) -> Self { fn from(err: std::str::Utf8Error) -> Self {