1use std::io;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
6pub enum Error {
7 #[error("IO error")]
8 IO(#[from] io::Error),
9 #[error("Not implemented")]
10 NotImplemented,
11 #[error("Config error {0}")]
12 Config(#[from] serde_json::Error),
13 #[error("Aborted by user")]
14 AbortedByUser,
15 #[error("Context error: {0:?}")]
16 Context(#[from] crate::context::Error),
17 #[error("{0:?}")]
18 Other(Box<dyn std::error::Error + Send + Sync + 'static>),
19}
20pub type Result<T, E = Error> = std::result::Result<T, E>;
21pub const NOT_IMPLEMENTED: Error = Error::NotImplemented;
22
23pub fn map_other(e: impl std::error::Error + Send + Sync + 'static) -> Error {
24 Error::Other(e.into())
25}
26
27impl From<Error> for io::Error {
28 fn from(e: Error) -> Self {
29 match e {
30 Error::IO(e) => e,
31 e => io::Error::new(io::ErrorKind::Other, e),
32 }
33 }
34}
35
36impl Error {
37 pub fn is_aborted(&self) -> bool {
38 match self {
39 Error::AbortedByUser => true,
40 _ => false,
41 }
42 }
43 pub fn is_addr_in_use(&self) -> bool {
44 match self {
45 Error::IO(e) => e.kind() == io::ErrorKind::AddrInUse,
46 _ => false,
47 }
48 }
49}