aboutsummaryrefslogtreecommitdiff
path: root/src/util/error.rs
blob: d2ed1ccfdc316922a20421b3156bb34d259629ca (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use err_derive::Error;
use hyper::StatusCode;
use std::io;

use crate::data::*;

#[derive(Debug, Error)]
pub enum RPCError {
	#[error(display = "Node is down: {:?}.", _0)]
	NodeDown(UUID),
	#[error(display = "Timeout: {}", _0)]
	Timeout(#[error(source)] tokio::time::Elapsed),
	#[error(display = "HTTP error: {}", _0)]
	HTTP(#[error(source)] http::Error),
	#[error(display = "Hyper error: {}", _0)]
	Hyper(#[error(source)] hyper::Error),
	#[error(display = "Messagepack encode error: {}", _0)]
	RMPEncode(#[error(source)] rmp_serde::encode::Error),
	#[error(display = "Messagepack decode error: {}", _0)]
	RMPDecode(#[error(source)] rmp_serde::decode::Error),
	#[error(display = "Too many errors: {:?}", _0)]
	TooManyErrors(Vec<String>),
}

#[derive(Debug, Error)]
pub enum Error {
	#[error(display = "IO error: {}", _0)]
	Io(#[error(source)] io::Error),

	#[error(display = "Hyper error: {}", _0)]
	Hyper(#[error(source)] hyper::Error),

	#[error(display = "HTTP error: {}", _0)]
	HTTP(#[error(source)] http::Error),

	#[error(display = "Invalid HTTP header value: {}", _0)]
	HTTPHeader(#[error(source)] http::header::ToStrError),

	#[error(display = "TLS error: {}", _0)]
	TLS(#[error(source)] rustls::TLSError),

	#[error(display = "PKI error: {}", _0)]
	PKI(#[error(source)] webpki::Error),

	#[error(display = "Sled error: {}", _0)]
	Sled(#[error(source)] sled::Error),

	#[error(display = "Messagepack encode error: {}", _0)]
	RMPEncode(#[error(source)] rmp_serde::encode::Error),
	#[error(display = "Messagepack decode error: {}", _0)]
	RMPDecode(#[error(source)] rmp_serde::decode::Error),
	#[error(display = "JSON error: {}", _0)]
	JSON(#[error(source)] serde_json::error::Error),
	#[error(display = "TOML decode error: {}", _0)]
	TomlDecode(#[error(source)] toml::de::Error),

	#[error(display = "Timeout: {}", _0)]
	RPCTimeout(#[error(source)] tokio::time::Elapsed),

	#[error(display = "Tokio join error: {}", _0)]
	TokioJoin(#[error(source)] tokio::task::JoinError),

	#[error(display = "RPC call error: {}", _0)]
	RPC(#[error(source)] RPCError),

	#[error(display = "Remote error: {} (status code {})", _0, _1)]
	RemoteError(String, StatusCode),

	#[error(display = "Bad request: {}", _0)]
	BadRequest(String),

	#[error(display = "Forbidden: {}", _0)]
	Forbidden(String),

	#[error(display = "Not found")]
	NotFound,

	#[error(display = "Corrupt data: does not match hash {:?}", _0)]
	CorruptData(Hash),

	#[error(display = "{}", _0)]
	Message(String),
}

impl Error {
	pub fn http_status_code(&self) -> StatusCode {
		match self {
			Error::BadRequest(_) => StatusCode::BAD_REQUEST,
			Error::NotFound => StatusCode::NOT_FOUND,
			Error::Forbidden(_) => StatusCode::FORBIDDEN,
			Error::RPC(_) => StatusCode::SERVICE_UNAVAILABLE,
			_ => StatusCode::INTERNAL_SERVER_ERROR,
		}
	}
}

impl From<sled::TransactionError<Error>> for Error {
	fn from(e: sled::TransactionError<Error>) -> Error {
		match e {
			sled::TransactionError::Abort(x) => x,
			sled::TransactionError::Storage(x) => Error::Sled(x),
		}
	}
}

impl<T> From<tokio::sync::watch::error::SendError<T>> for Error {
	fn from(_e: tokio::sync::watch::error::SendError<T>) -> Error {
		Error::Message(format!("Watch send error"))
	}
}

impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
	fn from(_e: tokio::sync::mpsc::error::SendError<T>) -> Error {
		Error::Message(format!("MPSC send error"))
	}
}

impl From<std::str::Utf8Error> for Error {
    fn from(e: std::str::Utf8Error) -> Error {
        Error::BadRequest(format!("Invalid UTF-8: {}", e))
    }
}

impl From<roxmltree::Error> for Error {
    fn from(e: roxmltree::Error) -> Error {
        Error::BadRequest(format!("Invalid XML: {}", e))
    }
}