Skip to main content

hyperi_rustlib/transport/
error.rs

1// Project:   hyperi-rustlib
2// File:      src/transport/error.rs
3// Purpose:   Transport error types
4// Language:  Rust
5//
6// License:   FSL-1.1-ALv2
7// Copyright: (c) 2026 HYPERI PTY LIMITED
8
9use std::fmt;
10
11/// Result type for transport operations.
12pub type TransportResult<T> = Result<T, TransportError>;
13
14/// Errors that can occur during transport operations.
15#[derive(Debug)]
16pub enum TransportError {
17    /// Configuration error (missing or invalid config).
18    Config(String),
19
20    /// Connection error (network, auth, etc.).
21    Connection(String),
22
23    /// Send operation failed.
24    Send(String),
25
26    /// Receive operation failed.
27    Recv(String),
28
29    /// Commit/acknowledge operation failed.
30    Commit(String),
31
32    /// Transport is closed or shutting down.
33    Closed,
34
35    /// Timeout waiting for operation.
36    Timeout,
37
38    /// Backpressure - transport cannot accept more messages.
39    Backpressure,
40
41    /// Internal transport error.
42    Internal(String),
43
44    /// Admin operation error (topic/partition management).
45    Admin(String),
46}
47
48impl fmt::Display for TransportError {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Self::Config(msg) => write!(f, "transport config error: {msg}"),
52            Self::Connection(msg) => write!(f, "transport connection error: {msg}"),
53            Self::Send(msg) => write!(f, "transport send error: {msg}"),
54            Self::Recv(msg) => write!(f, "transport receive error: {msg}"),
55            Self::Commit(msg) => write!(f, "transport commit error: {msg}"),
56            Self::Closed => write!(f, "transport closed"),
57            Self::Timeout => write!(f, "transport operation timed out"),
58            Self::Backpressure => write!(f, "transport backpressure"),
59            Self::Internal(msg) => write!(f, "transport internal error: {msg}"),
60            Self::Admin(msg) => write!(f, "transport admin error: {msg}"),
61        }
62    }
63}
64
65impl std::error::Error for TransportError {}
66
67impl TransportError {
68    /// Returns true if this error is recoverable (retry may succeed).
69    #[must_use]
70    pub fn is_recoverable(&self) -> bool {
71        matches!(self, Self::Timeout | Self::Backpressure)
72    }
73
74    /// Returns true if this error indicates the transport is unusable.
75    #[must_use]
76    pub fn is_fatal(&self) -> bool {
77        matches!(self, Self::Closed | Self::Config(_))
78    }
79}