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

refined error model (#2253)

This commit is contained in:
Rob Ede
2021-06-17 17:57:58 +01:00
committed by GitHub
parent bb0331ae28
commit 532f7b9923
69 changed files with 1498 additions and 901 deletions

View File

@ -1,7 +1,7 @@
use actix_http::Error;
use std::error::Error as StdError;
#[actix_web::main]
async fn main() -> Result<(), Error> {
async fn main() -> Result<(), Box<dyn StdError>> {
std::env::set_var("RUST_LOG", "actix_http=trace");
env_logger::init();

View File

@ -6,7 +6,6 @@ pub use actix_http::http::Error as HttpError;
pub use actix_http::ws::HandshakeError as WsHandshakeError;
pub use actix_http::ws::ProtocolError as WsProtocolError;
use actix_http::ResponseError;
use serde_json::error::Error as JsonError;
use actix_http::http::{header::HeaderValue, StatusCode};
@ -77,6 +76,3 @@ pub enum JsonPayloadError {
}
impl std::error::Error for JsonPayloadError {}
/// Return `InternalServerError` for `JsonPayloadError`
impl ResponseError for JsonPayloadError {}

View File

@ -1,21 +1,21 @@
use std::convert::TryFrom;
use std::net;
use std::rc::Rc;
use std::time::Duration;
use std::{convert::TryFrom, error::Error as StdError, net, rc::Rc, time::Duration};
use bytes::Bytes;
use futures_core::Stream;
use serde::Serialize;
use actix_http::body::Body;
use actix_http::http::header::IntoHeaderValue;
use actix_http::http::{Error as HttpError, HeaderMap, HeaderName, Method, Uri};
use actix_http::{Error, RequestHead};
use actix_http::{
body::Body,
http::{header::IntoHeaderValue, Error as HttpError, HeaderMap, HeaderName, Method, Uri},
RequestHead,
};
use crate::sender::{RequestSender, SendClientRequest};
use crate::ClientConfig;
use crate::{
sender::{RequestSender, SendClientRequest},
ClientConfig,
};
/// `FrozenClientRequest` struct represents clonable client request.
/// `FrozenClientRequest` struct represents cloneable client request.
/// It could be used to send same request multiple times.
#[derive(Clone)]
pub struct FrozenClientRequest {
@ -82,7 +82,7 @@ impl FrozenClientRequest {
pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
E: Into<Error> + 'static,
E: Into<Box<dyn StdError>> + 'static,
{
RequestSender::Rc(self.head.clone(), None).send_stream(
self.addr,
@ -207,7 +207,7 @@ impl FrozenSendBuilder {
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
E: Into<Error> + 'static,
E: Into<Box<dyn StdError>> + 'static,
{
if let Some(e) = self.err {
return e.into();

View File

@ -128,8 +128,7 @@ pub use self::sender::SendClientRequest;
/// An asynchronous HTTP and WebSocket client.
///
/// ## Examples
///
/// # Examples
/// ```
/// use awc::Client;
///

View File

@ -1,25 +1,26 @@
use std::convert::TryFrom;
use std::rc::Rc;
use std::time::Duration;
use std::{fmt, net};
use std::{convert::TryFrom, error::Error as StdError, fmt, net, rc::Rc, time::Duration};
use bytes::Bytes;
use futures_core::Stream;
use serde::Serialize;
use actix_http::body::Body;
use actix_http::http::header::{self, IntoHeaderPair};
use actix_http::http::{
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
use actix_http::{
body::Body,
http::{
header::{self, IntoHeaderPair},
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
},
RequestHead,
};
use actix_http::{Error, RequestHead};
#[cfg(feature = "cookies")]
use crate::cookie::{Cookie, CookieJar};
use crate::error::{FreezeRequestError, InvalidUrl};
use crate::frozen::FrozenClientRequest;
use crate::sender::{PrepForSendingError, RequestSender, SendClientRequest};
use crate::ClientConfig;
use crate::{
error::{FreezeRequestError, InvalidUrl},
frozen::FrozenClientRequest,
sender::{PrepForSendingError, RequestSender, SendClientRequest},
ClientConfig,
};
#[cfg(feature = "compress")]
const HTTPS_ENCODING: &str = "br, gzip, deflate";
@ -408,7 +409,7 @@ impl ClientRequest {
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
E: Into<Error> + 'static,
E: Into<Box<dyn StdError>> + 'static,
{
let slf = match self.prep_for_sending() {
Ok(slf) => slf,

View File

@ -1,6 +1,7 @@
use std::{
error::Error as StdError,
future::Future,
io, net,
net,
pin::Pin,
rc::Rc,
task::{Context, Poll},
@ -24,22 +25,30 @@ use serde::Serialize;
#[cfg(feature = "compress")]
use actix_http::{encoding::Decoder, http::header::ContentEncoding, Payload, PayloadStream};
use crate::connect::{ConnectRequest, ConnectResponse};
use crate::error::{FreezeRequestError, InvalidUrl, SendRequestError};
use crate::response::ClientResponse;
use crate::ClientConfig;
use crate::{
error::{FreezeRequestError, InvalidUrl, SendRequestError},
ClientConfig, ClientResponse, ConnectRequest, ConnectResponse,
};
#[derive(Debug, From)]
pub(crate) enum PrepForSendingError {
Url(InvalidUrl),
Http(HttpError),
Json(serde_json::Error),
Form(serde_urlencoded::ser::Error),
}
impl From<PrepForSendingError> for FreezeRequestError {
fn from(err: PrepForSendingError) -> FreezeRequestError {
match err {
PrepForSendingError::Url(e) => FreezeRequestError::Url(e),
PrepForSendingError::Http(e) => FreezeRequestError::Http(e),
PrepForSendingError::Url(err) => FreezeRequestError::Url(err),
PrepForSendingError::Http(err) => FreezeRequestError::Http(err),
PrepForSendingError::Json(err) => {
FreezeRequestError::Custom(Box::new(err), Box::new("json serialization error"))
}
PrepForSendingError::Form(err) => {
FreezeRequestError::Custom(Box::new(err), Box::new("form serialization error"))
}
}
}
}
@ -49,6 +58,12 @@ impl From<PrepForSendingError> for SendRequestError {
match err {
PrepForSendingError::Url(e) => SendRequestError::Url(e),
PrepForSendingError::Http(e) => SendRequestError::Http(e),
PrepForSendingError::Json(err) => {
SendRequestError::Custom(Box::new(err), Box::new("json serialization error"))
}
PrepForSendingError::Form(err) => {
SendRequestError::Custom(Box::new(err), Box::new("form serialization error"))
}
}
}
}
@ -209,8 +224,7 @@ impl RequestSender {
) -> SendClientRequest {
let body = match serde_json::to_string(value) {
Ok(body) => body,
// TODO: own error type
Err(e) => return Error::from(io::Error::new(io::ErrorKind::Other, e)).into(),
Err(err) => return PrepForSendingError::Json(err).into(),
};
if let Err(e) = self.set_header_if_none(header::CONTENT_TYPE, "application/json") {
@ -236,8 +250,7 @@ impl RequestSender {
) -> SendClientRequest {
let body = match serde_urlencoded::to_string(value) {
Ok(body) => body,
// TODO: own error type
Err(e) => return Error::from(io::Error::new(io::ErrorKind::Other, e)).into(),
Err(err) => return PrepForSendingError::Form(err).into(),
};
// set content-type
@ -266,7 +279,7 @@ impl RequestSender {
) -> SendClientRequest
where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
E: Into<Error> + 'static,
E: Into<Box<dyn StdError>> + 'static,
{
self.send_body(
addr,