spiral-sdk 0.1.12

Rust SDK for Spiral resource and table APIs
Documentation
//! Errors that scandal operations return, as the scandal contract declares them.

use spiral_contract::error::{ErrorEnvelope, new_error_instance_id};

pub use super::api::ScandalError;

impl ScandalError {
    /// The error as the contract's envelope, for a transport that carries one.
    pub fn to_wire(&self) -> ErrorEnvelope {
        self.envelope(new_error_instance_id())
    }

    /// The declared error an envelope carries, or an internal error describing one it does not.
    pub fn from_wire(envelope: &ErrorEnvelope) -> Self {
        Self::decode_envelope(envelope).unwrap_or_else(|| Self::Internal {
            message: envelope.to_string(),
        })
    }

    /// The message a caller sees, whatever the error's kind.
    pub fn message(&self) -> String {
        match self {
            Self::Unauthenticated { message }
            | Self::InvalidArgument { message }
            | Self::Conflict { message }
            | Self::BudgetExceeded { message }
            | Self::StorageUnavailable { message }
            | Self::Unavailable { message }
            | Self::Internal { message } => message.clone(),
            Self::PermissionDenied {
                operation,
                resource,
            } => format!("Permission `{operation}` denied on `{resource}`"),
            Self::NotFound { resource } => format!("Resource `{resource}` not found"),
        }
    }
}