ctb_workspace_ipc/
error.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! IPC transport and protocol error types and conversion utilities.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace prelude"
26)]
27use crate::utilities::*;
28
29use std::ffi::OsString;
30
31use thiserror::Error;
32
33use crate::protocol::Response;
34
35/// Canonical error type for this crate.
36#[derive(Debug, Error)]
37pub enum Error {
38    #[error("Transport error: {0}")]
39    Transport(String),
40    #[error("Serialization error: {0}")]
41    Serialization(String),
42    #[error("Authorization error: {0}")]
43    Authorization(String),
44    #[error("Capability denied: {0}")]
45    CapabilityDenied(String),
46    #[error("Process error: {0}")]
47    Process(String),
48    #[error("Timeout")]
49    Timeout,
50    #[error("Canceled")]
51    Canceled,
52    #[error("Not found")]
53    NotFound,
54    #[error("Unsupported: {0}")]
55    Unsupported(String),
56    #[error("Internal: {0}")]
57    Internal(String),
58}
59
60impl From<std::io::Error> for Error {
61    /// Converts a `std::io::Error` into the canonical Error type.
62    fn from(e: std::io::Error) -> Self {
63        Error::Transport(e.to_string())
64    }
65}
66
67impl From<anyhow::Error> for Error {
68    /// Converts a `anyhow::Error` into the canonical Error type.
69    fn from(e: anyhow::Error) -> Self {
70        Error::Internal(e.to_string())
71    }
72}
73
74impl From<std::num::TryFromIntError> for Error {
75    fn from(e: std::num::TryFromIntError) -> Self {
76        Error::Internal(e.to_string())
77    }
78}
79
80impl From<crate::protocol::RpcError> for Error {
81    fn from(e: crate::protocol::RpcError) -> Self {
82        Error::Internal(format!("RPC error {}: {}", e.code, e.message))
83    }
84}
85
86impl From<Response> for Error {
87    /// Converts an error `Response` into the canonical Error type.
88    fn from(resp: Response) -> Self {
89        match resp.error {
90            Some(error) => Error::from(error),
91            _ => Error::Internal("Expected error response".to_string()),
92        }
93    }
94}
95
96impl From<OsString> for Error {
97    fn from(e: OsString) -> Self {
98        Error::Internal(format!("OS string error: {e:?}"))
99    }
100}
101
102/// Convert `anyhow::Error` into crate Error, preserving context.
103pub(crate) fn to_ipc_error(e: anyhow::Error) -> Error {
104    e.into()
105}