Skip to main content

bitwarden_ipc/
error.rs

1use bitwarden_error::bitwarden_error;
2use thiserror::Error;
3
4use crate::rpc::error::RpcError;
5
6/// Error returned by [`IpcClient::start`](crate::IpcClient::start). Indicates that the IPC client
7/// is already running.
8#[derive(Debug, Error, Clone, PartialEq, Eq)]
9#[error("IPC client is already running")]
10#[bitwarden_error(basic)]
11pub struct AlreadyRunningError;
12
13/// Error returned by [`IpcClient::send`](crate::IpcClient::send). Wraps the underlying transport
14/// error as a string.
15#[derive(Debug, Error, Clone, PartialEq, Eq)]
16#[error("{0}")]
17pub struct SendError(pub(crate) String);
18
19#[derive(Debug, Error, Clone, PartialEq, Eq)]
20#[bitwarden_error(flat)]
21#[allow(missing_docs)]
22pub enum SubscribeError {
23    #[error("The IPC processing thread is not running")]
24    NotStarted,
25}
26
27#[derive(Debug, Error, PartialEq, Eq)]
28#[bitwarden_error(flat)]
29#[allow(missing_docs)]
30pub enum ReceiveError {
31    #[error("Failed to subscribe to the IPC channel: {0}")]
32    Channel(#[from] tokio::sync::broadcast::error::RecvError),
33
34    #[error("Timed out while waiting for a message: {0}")]
35    Timeout(#[from] tokio::time::error::Elapsed),
36
37    #[error("Cancelled while waiting for a message")]
38    Cancelled,
39}
40
41#[derive(Debug, Error, PartialEq, Eq)]
42#[bitwarden_error(flat)]
43#[allow(missing_docs)]
44pub enum TypedReceiveError {
45    #[error("Failed to subscribe to the IPC channel: {0}")]
46    Channel(#[from] tokio::sync::broadcast::error::RecvError),
47
48    #[error("Timed out while waiting for a message: {0}")]
49    Timeout(#[from] tokio::time::error::Elapsed),
50
51    #[error("Cancelled while waiting for a message")]
52    Cancelled,
53
54    #[error("Typing error: {0}")]
55    Typing(String),
56}
57
58impl From<ReceiveError> for TypedReceiveError {
59    fn from(value: ReceiveError) -> Self {
60        match value {
61            ReceiveError::Channel(e) => TypedReceiveError::Channel(e),
62            ReceiveError::Timeout(e) => TypedReceiveError::Timeout(e),
63            ReceiveError::Cancelled => TypedReceiveError::Cancelled,
64        }
65    }
66}
67
68#[derive(Debug, Error, PartialEq, Eq)]
69#[bitwarden_error(flat)]
70#[allow(missing_docs)]
71pub enum RequestError {
72    #[error(transparent)]
73    Subscribe(#[from] SubscribeError),
74
75    #[error(transparent)]
76    Receive(#[from] TypedReceiveError),
77
78    #[error("Timed out while waiting for a message: {0}")]
79    Timeout(#[from] tokio::time::error::Elapsed),
80
81    #[error("Failed to send message: {0}")]
82    Send(String),
83
84    #[error("Error occured on the remote target: {0}")]
85    Rpc(#[from] RpcError),
86}