mirror of
https://github.com/haraldh/chainerror.git
synced 2025-01-31 00:56:41 +01:00
feat: add new(Into<String>)
method for str_context!
types
Signed-off-by: Harald Hoyer <harald@hoyer.xyz>
This commit is contained in:
parent
cb9465f0df
commit
aaca6945b0
|
@ -17,7 +17,7 @@ fn func3() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
chainerror::str_context!(Func2Error);
|
chainerror::str_context!(Func2Error);
|
||||||
|
|
||||||
fn func2() -> chainerror::Result<(), Func2Error> {
|
fn func2() -> chainerror::Result<(), Func2Error> {
|
||||||
func3().context(Func2Error("func2 error: calling func3".to_string()))?;
|
func3().context(Func2Error::new("func2 error: calling func3"))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
chainerror::str_context!(Func1Error);
|
chainerror::str_context!(Func1Error);
|
||||||
|
|
||||||
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
func2().context(Func1Error("func1 error".to_string()))?;
|
func2().context(Func1Error::new("func1 error"))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ chainerror::str_context!(Func1ErrorFunc2);
|
||||||
chainerror::str_context!(Func1ErrorIO);
|
chainerror::str_context!(Func1ErrorIO);
|
||||||
|
|
||||||
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
func2().context(Func1ErrorFunc2("func1 error calling func2".to_string()))?;
|
func2().context(Func1ErrorFunc2::new("func1 error calling func2"))?;
|
||||||
let filename = "bar.txt";
|
let filename = "bar.txt";
|
||||||
do_some_io().context(Func1ErrorIO(format!("Error reading '{}'", filename)))?;
|
do_some_io().context(Func1ErrorIO(format!("Error reading '{}'", filename)))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -84,7 +84,7 @@ impl<T: 'static + Display + Debug> Error<T> {
|
||||||
/// chainerror::str_context!(Func1Error);
|
/// chainerror::str_context!(Func1Error);
|
||||||
///
|
///
|
||||||
/// fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
/// fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
/// func2().context(Func1Error("func1 error".into()))?;
|
/// func2().context(Func1Error::new("func1 error"))?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
@ -576,7 +576,7 @@ where
|
||||||
/// chainerror::str_context!(Func1Error);
|
/// chainerror::str_context!(Func1Error);
|
||||||
///
|
///
|
||||||
/// fn func1() -> Result<(), Box<dyn Error>> {
|
/// fn func1() -> Result<(), Box<dyn Error>> {
|
||||||
/// func2().context(Func1Error("func1 error".into()))?;
|
/// func2().context(Func1Error::new("func1 error"))?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// # if let Err(e) = func1() {
|
/// # if let Err(e) = func1() {
|
||||||
|
@ -595,6 +595,11 @@ macro_rules! str_context {
|
||||||
($e:ident) => {
|
($e:ident) => {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct $e(pub String);
|
pub struct $e(pub String);
|
||||||
|
impl $e {
|
||||||
|
pub fn new<S: Into<String>>(s: S) -> Self {
|
||||||
|
$e(s.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
impl ::std::fmt::Display for $e {
|
impl ::std::fmt::Display for $e {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
write!(f, "{}", self.0)
|
write!(f, "{}", self.0)
|
||||||
|
|
Loading…
Reference in a new issue