t_rust_less_lib/service/
error.rs

1use crate::secrets_store::SecretStoreError;
2use crate::{block_store::StoreError, clipboard::ClipboardError};
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5use zeroize::Zeroize;
6
7#[derive(Debug, Error, PartialEq, Eq, Serialize, Deserialize, Zeroize, Clone)]
8#[cfg_attr(feature = "with_specta", derive(specta::Type))]
9#[zeroize(drop)]
10pub enum ServiceError {
11  #[error("SecretsStoreError: {0}")]
12  SecretsStore(SecretStoreError),
13  #[error("StoreError: {0}")]
14  StoreError(StoreError),
15  #[error("IO: {0}")]
16  IO(String),
17  #[error("Mutex: {0}")]
18  Mutex(String),
19  #[error("Store with name {0} not found")]
20  StoreNotFound(String),
21  #[error("Clipboard closed")]
22  ClipboardClosed,
23  #[error("Functionality not available (on your platform)")]
24  NotAvailable,
25}
26
27pub type ServiceResult<T> = Result<T, ServiceError>;
28
29error_convert_from!(std::io::Error, ServiceError, IO(display));
30error_convert_from!(toml::de::Error, ServiceError, IO(display));
31error_convert_from!(SecretStoreError, ServiceError, SecretsStore(direct));
32error_convert_from!(StoreError, ServiceError, StoreError(direct));
33error_convert_from!(ClipboardError, ServiceError, IO(display));
34error_convert_from!(futures::task::SpawnError, ServiceError, IO(display));
35error_convert_from!(serde_json::Error, ServiceError, IO(display));
36error_convert_from!(rmp_serde::encode::Error, ServiceError, IO(display));
37error_convert_from!(rmp_serde::decode::Error, ServiceError, IO(display));
38
39impl<T> From<std::sync::PoisonError<T>> for ServiceError {
40  fn from(error: std::sync::PoisonError<T>) -> Self {
41    ServiceError::Mutex(format!("{error}"))
42  }
43}
44
45impl From<capnp::Error> for ServiceError {
46  fn from(error: capnp::Error) -> Self {
47    match error.kind {
48      capnp::ErrorKind::Failed => {
49        match serde_json::from_str::<ServiceError>(error.extra.trim_start_matches("remote exception: ")) {
50          Ok(service_error) => service_error,
51          _ => ServiceError::IO(format!("{error}")),
52        }
53      }
54      _ => ServiceError::IO(format!("{error}")),
55    }
56  }
57}
58
59impl From<ServiceError> for capnp::Error {
60  fn from(error: ServiceError) -> capnp::Error {
61    match serde_json::to_string(&error) {
62      Ok(json) => capnp::Error {
63        kind: capnp::ErrorKind::Failed,
64        extra: json,
65      },
66      _ => capnp::Error {
67        kind: capnp::ErrorKind::Failed,
68        extra: format!("{error}"),
69      },
70    }
71  }
72}