mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 12:48:00 +00:00
allow defensive operations to take a proof (#11353)
* allow defensive operations to take a proof * Update frame/support/src/traits/misc.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/bags-list/src/list/mod.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * Update frame/support/src/traits/misc.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * Update frame/support/src/traits/misc.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * Fix build * fix build again * fmt Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This commit is contained in:
@@ -50,6 +50,16 @@ macro_rules! defensive {
|
||||
$error
|
||||
);
|
||||
debug_assert!(false, "{}: {:?}", $crate::traits::DEFENSIVE_OP_INTERNAL_ERROR, $error);
|
||||
};
|
||||
($error:tt, $proof:tt) => {
|
||||
frame_support::log::error!(
|
||||
target: "runtime",
|
||||
"{}: {:?}: {:?}",
|
||||
$crate::traits::DEFENSIVE_OP_PUBLIC_ERROR,
|
||||
$error,
|
||||
$proof,
|
||||
);
|
||||
debug_assert!(false, "{}: {:?}: {:?}", $crate::traits::DEFENSIVE_OP_INTERNAL_ERROR, $error, $proof);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +112,10 @@ pub trait Defensive<T> {
|
||||
/// }
|
||||
/// ```
|
||||
fn defensive(self) -> Self;
|
||||
|
||||
/// Same as [`Defensive::defensive`], but it takes a proof as input, and displays it if the
|
||||
/// defensive operation has been triggered.
|
||||
fn defensive_proof(self, proof: &'static str) -> Self;
|
||||
}
|
||||
|
||||
/// Subset of methods similar to [`Defensive`] that can only work for a `Result`.
|
||||
@@ -184,6 +198,13 @@ impl<T> Defensive<T> for Option<T> {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn defensive_proof(self, proof: &'static str) -> Self {
|
||||
if self.is_none() {
|
||||
defensive!(proof);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: sp_std::fmt::Debug> Defensive<T> for Result<T, E> {
|
||||
@@ -229,6 +250,16 @@ impl<T, E: sp_std::fmt::Debug> Defensive<T> for Result<T, E> {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn defensive_proof(self, proof: &'static str) -> Self {
|
||||
match self {
|
||||
Ok(inner) => Ok(inner),
|
||||
Err(e) => {
|
||||
defensive!(e, proof);
|
||||
Err(e)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: sp_std::fmt::Debug> DefensiveResult<T, E> for Result<T, E> {
|
||||
|
||||
Reference in New Issue
Block a user