From 31f8aebce44d05508ae4da4a82b3c050452c57b7 Mon Sep 17 00:00:00 2001 From: Oleh Martsokha Date: Mon, 7 Aug 2023 11:34:01 +0200 Subject: [PATCH] Impl reqwest::Error methods (#96) * impl reqwest::Error methods * update changelog * use reqwest::StatusCode * typo * consistent comments * fn is_middleware() --- .gitignore | 10 ++- CHANGELOG.md | 3 + reqwest-middleware/src/error.rs | 119 ++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 96ef6c0..3a75e4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,10 @@ -/target +# OS +.DS_Store + +# IDE +.idea/ +.vscode/ + +# Rust Cargo.lock +/target diff --git a/CHANGELOG.md b/CHANGELOG.md index b134121..9ec6fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Added all `reqwest::Error` methods for `reqwest_middleware::Error` + ## [0.2.2] - 2023-05-11 ### Added diff --git a/reqwest-middleware/src/error.rs b/reqwest-middleware/src/error.rs index c276578..2ed8dfe 100644 --- a/reqwest-middleware/src/error.rs +++ b/reqwest-middleware/src/error.rs @@ -1,3 +1,4 @@ +use reqwest::{StatusCode, Url}; use thiserror::Error; pub type Result = std::result::Result; @@ -19,4 +20,122 @@ impl Error { { Error::Middleware(err.into()) } + + /// Returns a possible URL related to this error. + pub fn url(&self) -> Option<&Url> { + match self { + Error::Middleware(_) => None, + Error::Reqwest(e) => e.url(), + } + } + + /// Returns a mutable reference to the URL related to this error. + /// + /// This is useful if you need to remove sensitive information from the URL + /// (e.g. an API key in the query), but do not want to remove the URL + /// entirely. + pub fn url_mut(&mut self) -> Option<&mut Url> { + match self { + Error::Middleware(_) => None, + Error::Reqwest(e) => e.url_mut(), + } + } + + /// Adds a url related to this error (overwriting any existing). + pub fn with_url(self, url: Url) -> Self { + match self { + Error::Middleware(_) => self, + Error::Reqwest(e) => e.with_url(url).into(), + } + } + + /// Strips the related URL from this error (if, for example, it contains + /// sensitive information). + pub fn without_url(self) -> Self { + match self { + Error::Middleware(_) => self, + Error::Reqwest(e) => e.without_url().into(), + } + } + + /// Returns true if the error is from any middleware. + pub fn is_middleware(&self) -> bool { + match self { + Error::Middleware(_) => true, + Error::Reqwest(_) => false, + } + } + + /// Returns true if the error is from a type `Builder`. + pub fn is_builder(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_builder(), + } + } + + /// Returns true if the error is from a `RedirectPolicy`. + pub fn is_redirect(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_redirect(), + } + } + + /// Returns true if the error is from `Response::error_for_status`. + pub fn is_status(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_status(), + } + } + + /// Returns true if the error is related to a timeout. + pub fn is_timeout(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_timeout(), + } + } + + /// Returns true if the error is related to the request. + pub fn is_request(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_request(), + } + } + + #[cfg(not(target_arch = "wasm32"))] + /// Returns true if the error is related to connect. + pub fn is_connect(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_connect(), + } + } + + /// Returns true if the error is related to the request or response body. + pub fn is_body(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_body(), + } + } + + /// Returns true if the error is related to decoding the response's body. + pub fn is_decode(&self) -> bool { + match self { + Error::Middleware(_) => false, + Error::Reqwest(e) => e.is_decode(), + } + } + + /// Returns the status code, if the error was generated from a response. + pub fn status(&self) -> Option { + match self { + Error::Middleware(_) => None, + Error::Reqwest(e) => e.status(), + } + } }