Skip to main content

epics_base_rs/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum CaError {
5    #[error("I/O error: {0}")]
6    Io(#[from] std::io::Error),
7
8    #[error("timeout waiting for response")]
9    Timeout,
10
11    #[error("channel not found: {0}")]
12    ChannelNotFound(String),
13
14    #[error("protocol error: {0}")]
15    Protocol(String),
16
17    #[error("unsupported DBR type: {0}")]
18    UnsupportedType(u16),
19
20    #[error("write failed: ECA status {0:#06x}")]
21    WriteFailed(u32),
22
23    #[error("field not found: {0}")]
24    FieldNotFound(String),
25
26    #[error("field is read-only: {0}")]
27    ReadOnlyField(String),
28
29    #[error("type mismatch for field {0}")]
30    TypeMismatch(String),
31
32    #[error("invalid value: {0}")]
33    InvalidValue(String),
34
35    #[error("put disabled (DISP=1) for field {0}")]
36    PutDisabled(String),
37
38    #[error("link error: {0}")]
39    LinkError(String),
40
41    #[error("DB parse error at line {line}, column {column}: {message}")]
42    DbParseError {
43        line: usize,
44        column: usize,
45        message: String,
46    },
47
48    #[error("calc error: {0}")]
49    CalcError(String),
50
51    #[error("channel disconnected")]
52    Disconnected,
53
54    #[error("client shut down")]
55    Shutdown,
56
57    /// CA WRITE_NOTIFY arrived while a previous async put on the same
58    /// record is still in flight. Mirrors C EPICS `S_db_Blocked`; the
59    /// CA server replies `ECA_PUTCBINPROG`.
60    #[error("put callback in progress for record {0}")]
61    PutCallbackInProgress(String),
62}
63
64// ECA status constants (originally from protocol.rs, now in epics-ca-rs)
65const ECA_TIMEOUT: u32 = 80; // defmsg(CA_K_WARNING, 10)
66const ECA_NOWTACCESS: u32 = 376; // defmsg(CA_K_WARNING, 47)
67const ECA_PUTFAIL: u32 = 160; // defmsg(CA_K_WARNING, 20)
68const ECA_BADTYPE: u32 = 114; // defmsg(CA_K_ERROR, 14)
69const ECA_DISCONN: u32 = 192; // defmsg(CA_K_WARNING, 24)
70const ECA_PUTCBINPROG: u32 = 366; // defmsg(CA_K_ERROR, 45)
71
72impl CaError {
73    pub fn to_eca_status(&self) -> u32 {
74        match self {
75            CaError::Timeout => ECA_TIMEOUT,
76            CaError::ReadOnlyField(_) => ECA_NOWTACCESS,
77            CaError::PutDisabled(_) => ECA_PUTFAIL,
78            CaError::TypeMismatch(_) => ECA_BADTYPE,
79            CaError::UnsupportedType(_) => ECA_BADTYPE,
80            CaError::InvalidValue(_) => ECA_BADTYPE,
81            CaError::FieldNotFound(_) => ECA_PUTFAIL,
82            // Disconnection / shutdown are surfaced as ECA_DISCONN so a
83            // downstream client (e.g. caput on a CA gateway whose
84            // upstream just dropped) sees the actionable
85            // "CA channel disconnected" message rather than the
86            // catch-all "Put fail".
87            CaError::Disconnected | CaError::Shutdown => ECA_DISCONN,
88            // I/O errors usually mean the upstream connection is
89            // wedged; mapping to ECA_DISCONN matches operator
90            // expectations from C ca-gateway.
91            CaError::Io(_) => ECA_DISCONN,
92            CaError::WriteFailed(code) => *code,
93            CaError::PutCallbackInProgress(_) => ECA_PUTCBINPROG,
94            _ => ECA_PUTFAIL,
95        }
96    }
97}
98
99pub type CaResult<T> = Result<T, CaError>;