Skip to main content

ReplyError

Derive Macro ReplyError 

Source
#[derive(ReplyError)]
{
    // Attributes available to this derive:
    #[zlink]
}
Expand description

Derives ReplyError for enums, generating error definitions for Varlink service errors.

Requires the introspection feature to be enabled.

This macro generates implementations of the ReplyError trait, which provides a list of error variants that can be returned by a Varlink service method. It supports unit variants, variants with named fields, and single-field tuple variants (where the field type implements Type and has a Type::Object).

§Supported Attributes

The following attributes can be used to customize the behavior of this derive macro:

  • #[zlink(crate = "path")] - Specifies the crate path to use for zlink types. Defaults to ::zlink.

§Example

use zlink::introspect::ReplyError;

#[derive(ReplyError)]
enum ServiceError {
    // Unit variant - no parameters
    NotFound,

    // Named field variant - multiple parameters
    InvalidQuery {
        message: String,
        line: u32,
    },

    // Single tuple variant - uses fields from the wrapped type
    ValidationFailed(ValidationDetails),
}

// Example struct for tuple variant
#[derive(zlink::introspect::Type)]
struct ValidationDetails {
    field_name: String,
    expected: String,
}

// Access the generated error variants
assert_eq!(ServiceError::VARIANTS.len(), 3);
assert_eq!(ServiceError::VARIANTS[0].name(), "NotFound");
assert!(ServiceError::VARIANTS[0].has_no_fields());

assert_eq!(ServiceError::VARIANTS[1].name(), "InvalidQuery");
assert!(!ServiceError::VARIANTS[1].has_no_fields());