mirror of https://github.com/fafhrd91/actix-web
fix todo in client request prepare errors
This commit is contained in:
parent
03c03b608c
commit
f56def9f95
|
@ -1,5 +1,5 @@
|
|||
[alias]
|
||||
chk = "hack check --workspace --all-features --tests --examples"
|
||||
chk = "check --workspace --all-features --tests --examples --bins"
|
||||
lint = "clippy --workspace --tests --examples"
|
||||
ci-min = "hack check --workspace --no-default-features"
|
||||
ci-min-test = "hack check --workspace --no-default-features --tests --examples"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::io;
|
||||
use std::{error::Error as StdError, fmt, io};
|
||||
|
||||
use derive_more::{Display, From};
|
||||
|
||||
|
@ -10,6 +10,7 @@ use crate::http::Error as HttpError;
|
|||
|
||||
/// A set of errors that can occur while connecting to an HTTP host
|
||||
#[derive(Debug, Display, From)]
|
||||
#[non_exhaustive]
|
||||
pub enum ConnectError {
|
||||
/// SSL feature is not enabled
|
||||
#[display(fmt = "SSL is not supported")]
|
||||
|
@ -64,6 +65,7 @@ impl From<actix_tls::connect::ConnectError> for ConnectError {
|
|||
}
|
||||
|
||||
#[derive(Debug, Display, From)]
|
||||
#[non_exhaustive]
|
||||
pub enum InvalidUrl {
|
||||
#[display(fmt = "Missing URL scheme")]
|
||||
MissingScheme,
|
||||
|
@ -82,6 +84,7 @@ impl std::error::Error for InvalidUrl {}
|
|||
|
||||
/// A set of errors that can occur during request sending and response reading
|
||||
#[derive(Debug, Display, From)]
|
||||
#[non_exhaustive]
|
||||
pub enum SendRequestError {
|
||||
/// Invalid URL
|
||||
#[display(fmt = "Invalid URL: {}", _0)]
|
||||
|
@ -115,12 +118,17 @@ pub enum SendRequestError {
|
|||
|
||||
/// Error sending request body
|
||||
Body(Error),
|
||||
|
||||
/// Other errors that can occur after submitting a request.
|
||||
#[display(fmt = "{:?}: {}", _1, _0)]
|
||||
Custom(Box<dyn StdError>, Box<dyn fmt::Debug>),
|
||||
}
|
||||
|
||||
impl std::error::Error for SendRequestError {}
|
||||
|
||||
/// A set of errors that can occur during freezing a request
|
||||
#[derive(Debug, Display, From)]
|
||||
#[non_exhaustive]
|
||||
pub enum FreezeRequestError {
|
||||
/// Invalid URL
|
||||
#[display(fmt = "Invalid URL: {}", _0)]
|
||||
|
@ -129,15 +137,20 @@ pub enum FreezeRequestError {
|
|||
/// HTTP error
|
||||
#[display(fmt = "{}", _0)]
|
||||
Http(HttpError),
|
||||
|
||||
/// Other errors that can occur after submitting a request.
|
||||
#[display(fmt = "{:?}: {}", _1, _0)]
|
||||
Custom(Box<dyn StdError>, Box<dyn fmt::Debug>),
|
||||
}
|
||||
|
||||
impl std::error::Error for FreezeRequestError {}
|
||||
|
||||
impl From<FreezeRequestError> for SendRequestError {
|
||||
fn from(e: FreezeRequestError) -> Self {
|
||||
match e {
|
||||
FreezeRequestError::Url(e) => e.into(),
|
||||
FreezeRequestError::Http(e) => e.into(),
|
||||
fn from(err: FreezeRequestError) -> Self {
|
||||
match err {
|
||||
FreezeRequestError::Url(err) => err.into(),
|
||||
FreezeRequestError::Http(err) => err.into(),
|
||||
FreezeRequestError::Custom(err, msg) => SendRequestError::Custom(err, msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,13 +34,21 @@ use crate::{
|
|||
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"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,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"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -210,8 +224,7 @@ impl RequestSender {
|
|||
) -> SendClientRequest {
|
||||
let body = match serde_json::to_string(value) {
|
||||
Ok(body) => body,
|
||||
// TODO: own error type
|
||||
Err(_e) => todo!(),
|
||||
Err(err) => return PrepForSendingError::Json(err).into(),
|
||||
};
|
||||
|
||||
if let Err(e) = self.set_header_if_none(header::CONTENT_TYPE, "application/json") {
|
||||
|
@ -237,8 +250,7 @@ impl RequestSender {
|
|||
) -> SendClientRequest {
|
||||
let body = match serde_urlencoded::to_string(value) {
|
||||
Ok(body) => body,
|
||||
// TODO: own error type
|
||||
Err(_e) => todo!(),
|
||||
Err(err) => return PrepForSendingError::Form(err).into(),
|
||||
};
|
||||
|
||||
// set content-type
|
||||
|
|
Loading…
Reference in New Issue