Skip to main content

blvm_protocol/
error.rs

1//! Protocol-specific error types
2//!
3//! This module defines error types for the protocol layer that wrap consensus errors
4//! and add protocol-specific error context.
5
6use crate::ProtocolVersion;
7use std::borrow::Cow;
8use thiserror::Error;
9
10/// Protocol-specific error types
11///
12/// This enum wraps consensus errors and adds protocol-specific error types
13/// for better error context through the protocol layer.
14#[derive(Debug, Error, Clone, PartialEq)]
15pub enum ProtocolError {
16    /// Consensus validation error (wrapped from consensus layer)
17    #[error("Consensus error: {0}")]
18    Consensus(#[from] blvm_consensus::error::ConsensusError),
19
20    /// Protocol validation failed (size limits, feature flags, etc.)
21    #[error("Protocol validation failed: {0}")]
22    Validation(Cow<'static, str>),
23
24    /// Feature not supported by this protocol version
25    #[error("Feature not supported: {0}")]
26    UnsupportedFeature(String),
27
28    /// Protocol version mismatch
29    #[error("Protocol version mismatch: expected {expected:?}, got {actual:?}")]
30    VersionMismatch {
31        expected: ProtocolVersion,
32        actual: ProtocolVersion,
33    },
34
35    /// Network parameter error
36    #[error("Network parameter error: {0}")]
37    NetworkParameter(Cow<'static, str>),
38
39    /// Configuration error
40    #[error("Configuration error: {0}")]
41    Configuration(Cow<'static, str>),
42
43    /// Message size exceeds protocol limits
44    #[error("Message size exceeds protocol limit: {size} bytes (max {max} bytes)")]
45    MessageTooLarge { size: usize, max: usize },
46
47    /// Invalid protocol message
48    #[error("Invalid protocol message: {0}")]
49    InvalidMessage(Cow<'static, str>),
50
51    /// Service flag error
52    #[error("Service flag error: {0}")]
53    ServiceFlag(Cow<'static, str>),
54
55    /// Serialization error
56    #[error("Serialization error: {0}")]
57    Serialization(String),
58}
59
60/// Protocol-specific Result type
61pub type Result<T> = std::result::Result<T, ProtocolError>;