ctoolbox/workspace/ipc/
error.rs

1use std::ffi::OsString;
2
3use thiserror::Error;
4
5use crate::workspace;
6use crate::workspace::ipc::protocol::Response;
7
8/// Canonical error type for this crate.
9#[derive(Debug, Error)]
10pub enum Error {
11    #[error("Transport error: {0}")]
12    Transport(String),
13    #[error("Serialization error: {0}")]
14    Serialization(String),
15    #[error("Authorization error: {0}")]
16    Authorization(String),
17    #[error("Capability denied: {0}")]
18    CapabilityDenied(String),
19    #[error("Process error: {0}")]
20    Process(String),
21    #[error("Timeout")]
22    Timeout,
23    #[error("Canceled")]
24    Canceled,
25    #[error("Not found")]
26    NotFound,
27    #[error("Unsupported: {0}")]
28    Unsupported(String),
29    #[error("Internal: {0}")]
30    Internal(String),
31}
32
33impl From<std::io::Error> for Error {
34    /// Converts a `std::io::Error` into the canonical Error type.
35    fn from(e: std::io::Error) -> Self {
36        Error::Transport(e.to_string())
37    }
38}
39
40impl From<anyhow::Error> for Error {
41    /// Converts a `anyhow::Error` into the canonical Error type.
42    fn from(e: anyhow::Error) -> Self {
43        Error::Internal(e.to_string())
44    }
45}
46
47impl From<std::num::TryFromIntError> for Error {
48    fn from(e: std::num::TryFromIntError) -> Self {
49        Error::Internal(e.to_string())
50    }
51}
52
53impl From<workspace::ipc::protocol::RpcError> for Error {
54    fn from(e: workspace::ipc::protocol::RpcError) -> Self {
55        Error::Internal(format!("RPC error {}: {}", e.code, e.message))
56    }
57}
58
59impl From<Response> for Error {
60    /// Converts an error `Response` into the canonical Error type.
61    fn from(resp: Response) -> Self {
62        match resp.error {
63            Some(error) => Error::from(error),
64            _ => Error::Internal("Expected error response".to_string()),
65        }
66    }
67}
68
69impl From<OsString> for Error {
70    fn from(e: OsString) -> Self {
71        Error::Internal(format!("OS string error: {e:?}"))
72    }
73}