#error #wamp

battler-wamprat-error

Procedural macro for custom WAMP errors

3 releases

Uses new Rust 2024

0.1.2 Mar 19, 2025
0.1.1 Mar 19, 2025
0.1.0 Jan 24, 2025

#413 in WebSocket

Download history 117/week @ 2025-01-24 15/week @ 2025-01-31 5/week @ 2025-02-07 3/week @ 2025-02-14 7/week @ 2025-02-21 7/week @ 2025-02-28 1/week @ 2025-03-07 190/week @ 2025-03-14 87/week @ 2025-03-21 6/week @ 2025-03-28 4/week @ 2025-04-04

287 downloads per month
Used in 3 crates

MIT license

375KB
9K SLoC

battler-wamprat-error

battler-wamprat-error is a utility crate for battler-wamprat. It provides a procedural macro for custom WAMP errors.

battler_wamp and battler_wamprat use the battler_wamp::core::error::WampError type for errors transmitted between peers and routers. Errors are categorized by their URI and may be contextualized by an error message.

Custom WAMP error types can implement conversions to and from the core battler_wamp::core::error::WampError type for use in application code (and as a part of the battler_wamprat) framework. The WampError derive macro generates these conversions for you. It works for structs or enums. All structs and enum variants must have the following properties:

  1. Have a valid URI attribute for identification.
  2. Be a unit struct/variant or be constructible from a single [&str][str] (the error message).

Example

use battler_wamp::core::{
    error::WampError,
    uri::Uri,
};
use battler_wamprat_error::WampError;

#[derive(Debug, PartialEq, WampError)]
enum CustomError {
    #[uri("com.test.missing_input")]
    MissingInput,
    #[uri("com.test.failed")]
    Failed(String),
}

impl std::fmt::Display for CustomError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MissingInput => write!(f, "missing input!"),
            Self::Failed(msg) => write!(f, "{msg}"),
        }
    }
}

fn main() {
    // Into.
    assert_eq!(
        Into::<WampError>::into(CustomError::MissingInput),
        WampError::new(
            Uri::try_from("com.test.missing_input").unwrap(),
            "missing input!"
        )
    );
    assert_eq!(
        Into::<WampError>::into(CustomError::Failed("connection lost".to_owned())),
        WampError::new(Uri::try_from("com.test.failed").unwrap(), "connection lost")
    );

    // From.
    assert_eq!(
        TryInto::<CustomError>::try_into(WampError::new(
            Uri::try_from("com.test.missing_input").unwrap(),
            ""
        ))
        .unwrap(),
        CustomError::MissingInput
    );
    assert_eq!(
        TryInto::<CustomError>::try_into(WampError::new(
            Uri::try_from("com.test.failed").unwrap(),
            "deadline exceeded"
        ))
        .unwrap(),
        CustomError::Failed("deadline exceeded".to_owned())
    );
}

battler-wamprat-error

Latest Version

battler-wamprat-error is a utility crate for battler-wamprat. It provides a procedural macro for custom WAMP errors.

Dependencies

~19–30MB
~553K SLoC