Skip to main content

AssertErrorHasSource

Trait AssertErrorHasSource 

Source
pub trait AssertErrorHasSource<'a, R> {
    // Required methods
    fn has_no_source(self) -> Self;
    fn has_source(self) -> Self;
    fn has_source_message(
        self,
        expected_source_message: impl Into<String>,
    ) -> Spec<'a, Option<String>, R>;
}
Expand description

Assert the source of any type that implements std::error::Error.

§Examples

use asserting::prelude::*;
use std::error::Error;
use std::fmt::{self, Display};

#[derive(Debug)]
struct SuperError {
    source: SourceError,
}

impl Display for SuperError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "super-error caused by {}", self.source)
    }
}

impl Error for SuperError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.source)
    }
}

#[derive(Debug, PartialEq)]
enum SourceError {
    Foo,
    Bar,
}

impl Display for SourceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Foo => f.write_str("foo error"),
            Self::Bar => f.write_str("bar error"),
        }
    }
}

impl Error for SourceError {}

assert_that!(&SourceError::Foo).has_no_source();

let error = SuperError {
    source: SourceError::Foo,
};

assert_that!(&error).has_source();
assert_that!(&error).has_source_message("foo error");

Required Methods§

Source

fn has_no_source(self) -> Self

Verifies that an error has no source.

§Example
use asserting::prelude::*;
use std::error::Error;
use std::fmt::{self, Display};

#[derive(Debug, PartialEq)]
enum SimpleError {
    Foo,
    Bar,
}

impl Display for SimpleError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Foo => f.write_str("foo error"),
            Self::Bar => f.write_str("bar error"),
        }
    }
}

impl Error for SimpleError {}


let error = SimpleError::Bar;

assert_that!(&error).has_no_source();

// error in result
let result: Result<Vec<i32>, SimpleError> = Err(SimpleError::Foo);

assert_that!(&result).err().has_no_source();
Source

fn has_source(self) -> Self

Verifies that an error has some source.

§Example
use asserting::prelude::*;
use std::error::Error;
use std::fmt::{self, Display};

#[derive(Debug)]
struct SuperError {
    source: SourceError,
}

impl Display for SuperError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "super-error caused by {}", self.source)
    }
}

impl Error for SuperError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.source)
    }
}

#[derive(Debug, PartialEq)]
enum SourceError {
    Foo,
    Bar,
}

impl Display for SourceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Foo => f.write_str("foo error"),
            Self::Bar => f.write_str("bar error"),
        }
    }
}

impl Error for SourceError {}


let error = SuperError {
    source: SourceError::Foo,
};

assert_that!(&error).has_source();

// error in result
let result: Result<Vec<i32>, SuperError> = Err(SuperError {
    source: SourceError::Bar,
});

assert_that!(result).err().has_source();
Source

fn has_source_message( self, expected_source_message: impl Into<String>, ) -> Spec<'a, Option<String>, R>

Verifies that an error has some source which converted to a string equals the expected message.

§Example
use asserting::prelude::*;
use std::error::Error;
use std::fmt::{self, Display};

#[derive(Debug)]
struct SuperError {
    source: SourceError,
}

impl Display for SuperError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "super-error caused by {}", self.source)
    }
}

impl Error for SuperError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.source)
    }
}

#[derive(Debug, PartialEq)]
enum SourceError {
    Foo,
    Bar,
}

impl Display for SourceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Foo => f.write_str("foo error"),
            Self::Bar => f.write_str("bar error"),
        }
    }
}

impl Error for SourceError {}


let error = SuperError {
    source: SourceError::Bar,
};

assert_that!(&error).has_source_message("bar error");

// error in result
let result: Result<Vec<i32>, SuperError> = Err(SuperError {
    source: SourceError::Foo,
});

assert_that!(result).err().has_source_message("foo error");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a, S, R> AssertErrorHasSource<'a, R> for Spec<'a, S, R>
where S: Error, R: FailingStrategy,