Skip to main content

aranya_client/
error.rs

1//! Client API errors.
2
3use std::io;
4
5use aranya_daemon_api as api;
6use tarpc::client::RpcError;
7
8#[cfg(feature = "afc")]
9use crate::afc::Error as AfcError;
10
11/// The type returned by fallible Aranya operations.
12pub type Result<T, E = Error> = core::result::Result<T, E>;
13
14/// Possible errors that could happen in the Aranya client.
15#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum Error {
18    /// Unable to communicate with the daemon.
19    #[error("IPC error")]
20    Ipc(#[from] IpcError),
21
22    /// The daemon returned an error.
23    #[error("daemon error")]
24    Aranya(#[from] AranyaError),
25
26    /// A configuration error happened.
27    #[error("configuration error")]
28    Config(#[from] ConfigError),
29
30    /// An Aranya Fast Channel error happened.
31    #[cfg(feature = "afc")]
32    #[cfg_attr(docsrs, doc(cfg(feature = "afc")))]
33    #[error("AFC error")]
34    Afc(#[from] AfcError),
35
36    /// An unexpected internal error happened.
37    #[error(transparent)]
38    Bug(#[from] buggy::Bug),
39
40    /// Some other error occurred.
41    #[error(transparent)]
42    Other(#[from] OtherError),
43}
44
45impl From<core::convert::Infallible> for Error {
46    fn from(value: core::convert::Infallible) -> Self {
47        match value {}
48    }
49}
50
51/// Some other error occurred.
52#[derive(Debug, thiserror::Error)]
53#[error(transparent)]
54pub struct OtherError {
55    #[from]
56    err: anyhow::Error,
57}
58
59pub(crate) fn other<E>(err: E) -> OtherError
60where
61    E: Into<anyhow::Error>,
62{
63    OtherError { err: err.into() }
64}
65
66/// An Aranya error.
67#[derive(Debug, thiserror::Error)]
68#[error(transparent)]
69pub struct AranyaError {
70    #[from]
71    err: api::Error,
72}
73
74pub(crate) fn aranya_error(err: api::Error) -> Error {
75    Error::Aranya(err.into())
76}
77
78/// Possible errors that could happen when creating configuration info.
79#[derive(Debug, thiserror::Error)]
80#[non_exhaustive]
81pub enum ConfigError {
82    /// An invalid argument was provided.
83    #[error(transparent)]
84    InvalidArg(#[from] InvalidArg),
85}
86
87/// An invalid argument.
88#[derive(Debug, thiserror::Error)]
89#[error("invalid argument `{arg}`: {reason}")]
90pub struct InvalidArg {
91    arg: &'static str,
92    reason: &'static str,
93}
94
95impl InvalidArg {
96    pub(crate) const fn new(arg: &'static str, reason: &'static str) -> Self {
97        Self { arg, reason }
98    }
99}
100
101/// An IPC error.
102#[derive(Debug, thiserror::Error)]
103#[error(transparent)]
104pub struct IpcError(#[from] pub(crate) IpcRepr);
105
106impl IpcError {
107    pub(crate) fn new<E>(err: E) -> Self
108    where
109        E: Into<IpcRepr>,
110    {
111        Self(err.into())
112    }
113}
114
115#[derive(Debug, thiserror::Error)]
116#[error(transparent)]
117pub(crate) enum IpcRepr {
118    InvalidArg(#[from] InvalidArg),
119    Io(#[from] io::Error),
120    Tarpc(#[from] RpcError),
121    Other(#[from] anyhow::Error),
122}