Skip to main content

opcda_bridge/
error.rs

1//! The error type returned by every [`crate::Client`] method.
2
3/// Errors that can occur while talking to an opcda-bridge gateway.
4///
5/// Both variants use `#[error(transparent)]`: `Display` and `source()`
6/// forward straight through to the wrapped `tonic` error with no added
7/// prefix or extra chain link. This matters because `opcda-bridge-client`'s
8/// CLI commands convert this type into an `anyhow::Error` with a bare `?`
9/// (the same way they converted a raw `tonic::Status`/
10/// `tonic::transport::Error` before this crate existed); transparency keeps
11/// that conversion rendering byte-for-byte the same error text the CLI
12/// printed before this crate existed, which is part of this crate's
13/// contract with its CLI consumer and is pinned down by
14/// `test_error_rpc_anyhow_debug_matches_bare_status` and
15/// `client::tests::test_connect_failure_anyhow_debug_matches_bare_transport_error`.
16///
17/// A dedicated `thiserror` enum (rather than reusing `anyhow::Error` here
18/// the way `opcda-bridge-client`'s own commands do) still lets a downstream
19/// consumer that does *not* want an `anyhow` dependency match on
20/// [`Error::Connect`] / [`Error::Rpc`] directly.
21#[derive(Debug, thiserror::Error)]
22pub enum Error {
23    /// Failed to establish the gRPC channel to the gateway (e.g. connection
24    /// refused, DNS failure, invalid address).
25    #[error(transparent)]
26    Connect(#[from] tonic::transport::Error),
27    /// The gateway returned a gRPC error for a `ListServers`/`Browse`/
28    /// `Read`/`Write` call, or for a `Browse` response-stream item.
29    #[error(transparent)]
30    Rpc(#[from] tonic::Status),
31}
32
33/// A `Result` alias using [`Error`], mirroring the ergonomics of
34/// `anyhow::Result` (`opcda_bridge::Result<T>`) for a crate that
35/// intentionally does not depend on `anyhow` itself.
36pub type Result<T> = std::result::Result<T, Error>;
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_error_rpc_display_is_transparent() {
44        let status = tonic::Status::internal("boom");
45        let expected = status.to_string();
46        let err: Error = status.into();
47        assert_eq!(err.to_string(), expected);
48    }
49
50    #[test]
51    fn test_error_rpc_matches_variant() {
52        let status = tonic::Status::not_found("missing");
53        let err: Error = status.into();
54        assert!(matches!(err, Error::Rpc(_)));
55    }
56
57    #[test]
58    fn test_error_rpc_debug_matches_variant_and_status() {
59        let status = tonic::Status::internal("boom");
60        let err: Error = status.clone().into();
61        assert_eq!(format!("{err:?}"), format!("Rpc({status:?})"));
62    }
63
64    #[test]
65    fn test_error_rpc_anyhow_debug_matches_bare_status() {
66        // `opcda-bridge-client`'s commands convert this crate's `Error` into
67        // `anyhow::Error` via a bare `?`; this must render identically to
68        // today's direct `tonic::Status` -> `anyhow::Error` conversion, or
69        // the CLI's printed error text would silently change.
70        let status = tonic::Status::internal("boom");
71        let bare = anyhow::Error::from(status.clone());
72        let wrapped = anyhow::Error::from(Error::from(status));
73        assert_eq!(format!("{bare:?}"), format!("{wrapped:?}"));
74        assert_eq!(bare.to_string(), wrapped.to_string());
75    }
76
77    // `Error::Connect`'s transparency (both the plain and anyhow-wrapped
78    // rendering) is exercised in `client::tests`, since a real
79    // `tonic::transport::Error` can only be produced by an actual failed
80    // connection attempt, not constructed directly.
81}