t_rust_less_lib/service/
error.rs

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