AppErr

Struct AppErr 

Source
pub struct AppErr(/* private fields */);
Expand description

An error type used to pass an application-specific error through a library runtime.

Application callbacks can return this type for the Err() case in order to allow application-defined errors to be passed back to the application through a library runtime.

Implementations§

Source§

impl AppErr

Source

pub fn new<E>(e: E) -> Self
where E: Blessed + Send + 'static,

Create a new application callback error object that can be converted back into to its original type (as long as it is Any compatible).

use std::fmt;
use apperr::{AppErr, Blessed};

#[derive(Debug)]
enum MyErr {
  Something(String)
}
impl fmt::Display for MyErr {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      MyErr::Something(s) => {
        write!(f, "Some error; {}", s)
      }
    }
  }
}
impl std::error::Error for MyErr { }
impl Blessed for MyErr { }

let apperr = AppErr::new(MyErr::Something("hello".into()));
Source

pub fn is<T>(&self) -> bool
where T: Any,

Inspect error type wrapped by the AppErr.

use std::fmt;
use apperr::{AppErr, Blessed};

#[derive(Debug)]
enum MyErr {
  Something(String)
}
impl fmt::Display for MyErr {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      MyErr::Something(s) => {
        write!(f, "Some error; {}", s)
      }
    }
  }
}
impl std::error::Error for MyErr { }
impl Blessed for MyErr { }

let apperr = AppErr::new(MyErr::Something("hello".into()));

assert!(apperr.is::<MyErr>());
assert_eq!(apperr.is::<String>(), false);
Source

pub fn try_into_inner<E: 'static>(self) -> Result<E, AppErr>

Attempt to unpack and cast the inner error type.

If it can’t be downcast to E, the AppErr will be returned back to the caller in the Err() case.

use std::fmt;
use apperr::{AppErr, Blessed};

#[derive(Debug)]
enum MyErr {
  Something(String)
}
impl fmt::Display for MyErr {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      MyErr::Something(s) => {
        write!(f, "Some error; {}", s)
      }
    }
  }
}
impl std::error::Error for MyErr { }
impl Blessed for MyErr { }

let apperr = AppErr::new(MyErr::Something("hello".into()));

let Ok(e) = apperr.try_into_inner::<MyErr>() else {
  panic!("Unexpectedly not MyErr");
};
Source

pub fn unwrap_inner<E: 'static>(self) -> E

Unwrap application-specific error.

use std::fmt;
use apperr::{AppErr, Blessed};

#[derive(Debug)]
enum MyErr {
  Something(String)
}
impl fmt::Display for MyErr {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      MyErr::Something(s) => {
        write!(f, "Some error; {}", s)
      }
    }
  }
}
impl std::error::Error for MyErr { }
impl Blessed for MyErr { }

let apperr = AppErr::new(MyErr::Something("hello".into()));

let MyErr::Something(e) = apperr.unwrap_inner::<MyErr>() else {
  panic!("Unexpectedly not MyErr::Something");
};
assert_eq!(e, "hello");
§Panic

Panics if the inner type is not castable to E.

Trait Implementations§

Source§

impl Debug for AppErr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for AppErr

§

impl !RefUnwindSafe for AppErr

§

impl Send for AppErr

§

impl !Sync for AppErr

§

impl Unpin for AppErr

§

impl !UnwindSafe for AppErr

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.