From 117392d71343944008cea9a27e749260bb6d4721 Mon Sep 17 00:00:00 2001 From: hu-kai45 Date: Thu, 25 Apr 2024 15:33:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=20Upload=20Error=20=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hu-kai45 --- ylong_http_client/src/async_impl/conn/http1.rs | 9 ++++++++- ylong_http_client/src/error.rs | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ylong_http_client/src/async_impl/conn/http1.rs b/ylong_http_client/src/async_impl/conn/http1.rs index fd74173..49e5888 100644 --- a/ylong_http_client/src/async_impl/conn/http1.rs +++ b/ylong_http_client/src/async_impl/conn/http1.rs @@ -194,7 +194,14 @@ where Ok(size) => written += size, Err(e) => { conn.shutdown(); - return err_from_other!(BodyTransfer, e); + + let error = e.into(); + // When using `Uploader`, here we can get `UserAborted` error. + return if error.source().is_some() { + Err(HttpClientError::user_aborted()) + } else { + err_from_other!(BodyTransfer, error) + }; } } } diff --git a/ylong_http_client/src/error.rs b/ylong_http_client/src/error.rs index c786abe..cf4dcd0 100644 --- a/ylong_http_client/src/error.rs +++ b/ylong_http_client/src/error.rs @@ -15,6 +15,7 @@ //! this crate. use core::fmt::{Debug, Display, Formatter}; +use std::sync::Once; use std::{error, io}; /// The structure encapsulates errors that can be encountered when working with @@ -148,7 +149,21 @@ impl Display for HttpClientError { } } -impl error::Error for HttpClientError {} +impl error::Error for HttpClientError { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + static mut USER_ABORTED: Option> = None; + static ONCE: Once = Once::new(); + + ONCE.call_once(|| { + unsafe { USER_ABORTED = Some(Box::new(HttpClientError::user_aborted())) }; + }); + + if self.kind == ErrorKind::UserAborted { + return unsafe { USER_ABORTED.as_ref().map(|e| e.as_ref()) }; + } + None + } +} /// Error kinds which can indicate the type of a `HttpClientError`. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -- Gitee