Skip to main content

BevyError

Struct BevyError 

Source
pub struct BevyError { /* private fields */ }
Expand description

The built in “universal” Bevy error type. This has a blanket From impl for any type that implements Rust’s Error, meaning it can be used as a “catch all” error.

§Severity

Each BevyError carries a Severity value that indicates how serious the error is. While the levels within Severity correspond to traditional logging levels, these levels are fundamentally advisory metadata. The fallback error handler ultimately has discretion to respond to each of these errors according to its configuration. The error handler ultimately has discretion to respond to each of these errors according to its configuration. You can change the behavior of the fallback handler by modifying the FallbackErrorHandler resource.

By default, errors without an assigned severity use Severity::Panic, and will cause your application to panic. You can change the severity of an error by using with_severity, or map_severity on any Result type.

§Backtraces

When used with the backtrace Cargo feature, it can capture a backtrace when the error is constructed (generally in the From impl).

To enable backtrace capture on supported platforms, set the RUST_BACKTRACE environment variable. See Backtrace::capture for details.

When the error is printed, the backtrace will be displayed. By default, the backtrace will be trimmed down to filter out noise. To see the full backtrace, set the BEVY_BACKTRACE=full environment variable.

§Context

You can attach a context message to a Result or Option value to turn it into a Result with a BevyError using context or with_context. The resulting error will have the message passed to context added to it.

§Usage


fn fallible_system() -> Result<(), BevyError> {
    // This will result in Rust's built-in ParseIntError, which will automatically
    // be converted into a BevyError with an additional message.
    let parsed: usize = "I am not a number".parse().context("failed to parse number")?;
    Ok(())
}

Implementations§

Source§

impl BevyError

Source

pub fn new<E>(severity: Severity, error: E) -> Self
where Box<dyn Error + Sync + Send>: From<E>,

Constructs a new BevyError with the given Severity.

The error will be stored as a Box<dyn Error + Send + Sync>.

The easiest way to use this is to pass in a string. This works because any type that can be converted into a Box<dyn Error + Send + Sync> can be used, and str is one such type.

§Examples

fn some_function(val: i64) -> Result<(), BevyError> {
    if val < 0 {
        // Consider using the bevy_error! or even the bail! macro for format! support
        let error =
            BevyError::new(Severity::Panic, format!("Value can't be negative {val}"));
        return Err(error);
    }

    // ...
    Ok(())
}
Source

pub fn new_with_backtrace<E>( severity: Severity, error: E, backtrace: Backtrace, ) -> Self
where Box<dyn Error + Sync + Send>: From<E>,

Constructs a new BevyError with the given Severity.

Like BevyError::new, but if the backtrace cargo feature is enabled it will use the supplied backtrace instead of capturing a new one.

Source

pub fn ignore<E>(error: E) -> Self
where Box<dyn Error + Send + Sync>: From<E>,

Creates a new BevyError with the Severity::Ignore severity.

This is a shorthand for BevyError::new(Severity::Ignore, error).

Source

pub fn trace<E>(error: E) -> Self
where Box<dyn Error + Send + Sync>: From<E>,

Creates a new BevyError with the Severity::Trace severity.

This is a shorthand for BevyError::new(Severity::Trace, error).

Source

pub fn debug<E>(error: E) -> Self
where Box<dyn Error + Send + Sync>: From<E>,

Creates a new BevyError with the Severity::Debug severity.

This is a shorthand for BevyError::new(Severity::Debug, error).

Source

pub fn info<E>(error: E) -> Self
where Box<dyn Error + Send + Sync>: From<E>,

Creates a new BevyError with the Severity::Info severity.

This is a shorthand for BevyError::new(Severity::Info, error).

Source

pub fn warning<E>(error: E) -> Self
where Box<dyn Error + Send + Sync>: From<E>,

Creates a new BevyError with the Severity::Warning severity.

This is a shorthand for BevyError::new(Severity::Warning, error).

Source

pub fn error<E>(error: E) -> Self
where Box<dyn Error + Send + Sync>: From<E>,

Creates a new BevyError with the Severity::Error severity.

This is a shorthand for BevyError::new(Severity::Error, error).

Source

pub fn panic<E>(error: E, payload: Box<dyn Any + Send>) -> Self
where Box<dyn Error + Send + Sync>: From<E>,

Creates a new BevyError with the Severity::Panic severity.

This is a shorthand for BevyError::new(Severity::Panic, error).

Source

pub fn is<E: Error + 'static>(&self) -> bool

Checks if the internal error is of the given type.

Source

pub fn downcast_ref<E: Error + 'static>(&self) -> Option<&E>

Attempts to downcast the internal error to the given type.

Source§

impl BevyError

Source

pub fn severity(&self) -> Severity

Returns the severity of this error.

Source

pub fn with_severity(self, severity: Severity) -> Self

Returns this error with its severity overridden.

Note that this doesn’t change the underlying error value; only the Severity metadata used by the error handler.

Source

pub fn with_payload(self, payload: Box<dyn Any + Send>) -> Self

Adds a panic payload to the error. This allows the panic to be resumed with Self::take_payload in the error handler.

Source

pub fn take_payload(&mut self) -> Option<Box<dyn Any + Send>>

Use in an error handler to take the payload and use it to resume unwinding or add it to logging.

Trait Implementations§

Source§

impl Debug for BevyError

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for BevyError

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<E> From<E> for BevyError
where Box<dyn Error + Send + Sync + 'static>: From<E>,

Source§

fn from(error: E) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> ConditionalSend for T
where T: Send,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

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

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.