1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

big clean up and docs improvmenet of types mod (#1894)

This commit is contained in:
Rob Ede
2021-01-09 13:17:19 +00:00
committed by GitHub
parent 530d03791d
commit 6575ee93f2
22 changed files with 674 additions and 743 deletions

View File

@ -100,10 +100,6 @@ impl fmt::Debug for Error {
}
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
None
}
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
@ -309,28 +305,45 @@ impl From<httparse::Error> for ParseError {
pub enum PayloadError {
/// A payload reached EOF, but is not complete.
#[display(
fmt = "A payload reached EOF, but is not complete. With error: {:?}",
fmt = "A payload reached EOF, but is not complete. Inner error: {:?}",
_0
)]
Incomplete(Option<io::Error>),
/// Content encoding stream corruption
/// Content encoding stream corruption.
#[display(fmt = "Can not decode content-encoding.")]
EncodingCorrupted,
/// A payload reached size limit.
#[display(fmt = "A payload reached size limit.")]
/// Payload reached size limit.
#[display(fmt = "Payload reached size limit.")]
Overflow,
/// A payload length is unknown.
#[display(fmt = "A payload length is unknown.")]
/// Payload length is unknown.
#[display(fmt = "Payload length is unknown.")]
UnknownLength,
/// Http2 payload error
/// HTTP/2 payload error.
#[display(fmt = "{}", _0)]
Http2Payload(h2::Error),
/// Io error
/// Generic I/O error.
#[display(fmt = "{}", _0)]
Io(io::Error),
}
impl std::error::Error for PayloadError {}
impl std::error::Error for PayloadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
PayloadError::Incomplete(None) => None,
PayloadError::Incomplete(Some(err)) => Some(err as &dyn std::error::Error),
PayloadError::EncodingCorrupted => None,
PayloadError::Overflow => None,
PayloadError::UnknownLength => None,
PayloadError::Http2Payload(err) => Some(err as &dyn std::error::Error),
PayloadError::Io(err) => Some(err as &dyn std::error::Error),
}
}
}
impl From<h2::Error> for PayloadError {
fn from(err: h2::Error) -> Self {
@ -1009,22 +1022,22 @@ mod tests {
fn test_payload_error() {
let err: PayloadError =
io::Error::new(io::ErrorKind::Other, "ParseError").into();
assert!(format!("{}", err).contains("ParseError"));
assert!(err.to_string().contains("ParseError"));
let err = PayloadError::Incomplete(None);
assert_eq!(
format!("{}", err),
"A payload reached EOF, but is not complete. With error: None"
err.to_string(),
"A payload reached EOF, but is not complete. Inner error: None"
);
}
macro_rules! from {
($from:expr => $error:pat) => {
match ParseError::from($from) {
e @ $error => {
assert!(format!("{}", e).len() >= 5);
err @ $error => {
assert!(err.to_string().len() >= 5);
}
e => unreachable!("{:?}", e),
err => unreachable!("{:?}", err),
}
};
}
@ -1067,7 +1080,7 @@ mod tests {
let err = PayloadError::Overflow;
let resp_err: &dyn ResponseError = &err;
let err = resp_err.downcast_ref::<PayloadError>().unwrap();
assert_eq!(err.to_string(), "A payload reached size limit.");
assert_eq!(err.to_string(), "Payload reached size limit.");
let not_err = resp_err.downcast_ref::<ContentTypeError>();
assert!(not_err.is_none());
}

View File

@ -481,15 +481,14 @@ impl ResponseBuilder {
self
}
/// Set response content type
/// Set response content type.
#[inline]
pub fn content_type<V>(&mut self, value: V) -> &mut Self
where
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
V: IntoHeaderValue,
{
if let Some(parts) = parts(&mut self.head, &self.err) {
match HeaderValue::try_from(value) {
match value.try_into() {
Ok(value) => {
parts.headers.insert(header::CONTENT_TYPE, value);
}
@ -802,7 +801,7 @@ impl From<ResponseBuilder> for Response {
impl From<&'static str> for Response {
fn from(val: &'static str) -> Self {
Response::Ok()
.content_type("text/plain; charset=utf-8")
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(val)
}
}
@ -810,7 +809,7 @@ impl From<&'static str> for Response {
impl From<&'static [u8]> for Response {
fn from(val: &'static [u8]) -> Self {
Response::Ok()
.content_type("application/octet-stream")
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(val)
}
}
@ -818,7 +817,7 @@ impl From<&'static [u8]> for Response {
impl From<String> for Response {
fn from(val: String) -> Self {
Response::Ok()
.content_type("text/plain; charset=utf-8")
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(val)
}
}
@ -826,7 +825,7 @@ impl From<String> for Response {
impl<'a> From<&'a String> for Response {
fn from(val: &'a String) -> Self {
Response::Ok()
.content_type("text/plain; charset=utf-8")
.content_type(mime::TEXT_PLAIN_UTF_8)
.body(val)
}
}
@ -834,7 +833,7 @@ impl<'a> From<&'a String> for Response {
impl From<Bytes> for Response {
fn from(val: Bytes) -> Self {
Response::Ok()
.content_type("application/octet-stream")
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(val)
}
}
@ -842,7 +841,7 @@ impl From<Bytes> for Response {
impl From<BytesMut> for Response {
fn from(val: BytesMut) -> Self {
Response::Ok()
.content_type("application/octet-stream")
.content_type(mime::APPLICATION_OCTET_STREAM)
.body(val)
}
}

View File

@ -89,7 +89,7 @@ impl Codec {
/// Set max frame size.
///
/// By default max size is set to 64kb.
/// By default max size is set to 64kB.
pub fn max_size(mut self, size: usize) -> Self {
self.max_size = size;
self