summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: f987d663ac986cdd211338d1a48e985bbfaf9700 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use bincode::Error as bincode_e;
use hyper::Error as hyper_e;
use sled::Error as sled_e;
use std::fmt;
use std::net::AddrParseError;
use std::num::ParseIntError;

#[derive(Debug)]
pub struct TwinHError(pub String);

impl TwinHError {
    fn bail(self) {}
}

impl std::error::Error for TwinHError {}

impl std::fmt::Display for TwinHError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "twinh: {}", self.0)
    }
}

impl From<sled_e> for TwinHError {
    fn from(e: sled_e) -> Self {
        Self(format!("database error: {}", e))
    }
}

impl From<bincode_e> for TwinHError {
    fn from(e: bincode_e) -> Self {
        Self(format!("(de)serialization error: {}", e))
    }
}

impl From<AddrParseError> for TwinHError {
    fn from(e: AddrParseError) -> Self {
        Self(format!("failed to parse addr: {}", e))
    }
}

impl From<ParseIntError> for TwinHError {
    fn from(e: ParseIntError) -> Self {
        Self(format!("failed to parse port: {}", e))
    }
}

impl From<hyper_e> for TwinHError {
    fn from(e: hyper_e) -> Self {
        Self(format!("server error: {}", e))
    }
}