From 21df1a2f5202debe9f222dee38ce5e793f14a861 Mon Sep 17 00:00:00 2001 From: x Date: Thu, 24 Aug 2023 11:16:26 +0200 Subject: [PATCH] add assert_err macro --- src/error.rs | 14 ++++++++++++++ src/net/p2p.rs | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 18f2aaef7..76bf88690 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,6 +16,20 @@ * along with this program. If not, see . */ +// assert_err!(your_func(), Err(Error::UrlParsingFailed(_))); +// assert_err!(your_func(), Err(Error::CanonicalizationFailed(_))); +// assert_err!(your_func(), Err(Error::FileOpenFailed(er)) if er.kind() == ErrorKind::NotFound); +#[macro_export] +macro_rules! assert_err { + ($expression:expr, $($pattern:tt)+) => { + match $expression { + $($pattern)+ => (), + ref e => panic!("expected `{}` but got `{:?}`", stringify!($($pattern)+), e), + } + } +} +pub use assert_err; + // Hello developer. Please add your error to the according subsection // that is commented, or make a new subsection. Keep it clean. diff --git a/src/net/p2p.rs b/src/net/p2p.rs index 4655fa0a1..5e7123ea4 100644 --- a/src/net/p2p.rs +++ b/src/net/p2p.rs @@ -40,6 +40,7 @@ use super::{ settings::{Settings, SettingsPtr}, }; use crate::{ + error::assert_err, system::{ sleep_forever, StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription, }, @@ -186,7 +187,7 @@ impl P2p { async fn handle_stop(self: Arc, result: Result<()>) { assert!(result.is_err()); - //assert_eq!(result.unwrap_err(), Error::NetworkServiceStopped); + assert_err!(result, Err(Error::NetworkServiceStopped)); info!(target: "net::p2p::handle_stop()", "[P2P] Received stop signal. Shutting down."); // Stop the sessions self.session_manual().await.stop().await;