Skip to main content

RpcError

Derive Macro RpcError 

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

Derive macro for RPC errors

Generates From<YourError> for ServiceError and IntoServiceError implementations.

Use #[rpc(status = "...", code = ...)] attributes on variants to customize the RPC status and error code.

§Example

use synapse_macros::RpcError;

#[derive(Debug, thiserror::Error, RpcError)]
pub enum UserError {
    #[error("user not found: {0}")]
    #[rpc(status = "Error", code = 404)]
    NotFound(String),

    #[error("invalid email format")]
    #[rpc(status = "InvalidRequest", code = 400)]
    InvalidEmail,

    #[error("user is banned")]
    #[rpc(code = 3001)]  // defaults to Error status
    Banned,
}

§Available statuses

  • Ok - Success
  • Error - Generic error (default)
  • Timeout - Request timed out
  • InterfaceNotFound - Interface not found
  • MethodNotFound - Method not found
  • Unavailable - Service unavailable
  • InvalidRequest - Bad request
  • UnsupportedVersion - Unsupported protocol version
  • ServiceDraining - Service is draining
  • ServiceFrozen - Service is frozen
  • MessageTooLarge - Message too large