garage/src/util/error.rs

113 lines
3.2 KiB
Rust
Raw Normal View History

2020-04-09 15:32:28 +00:00
use err_derive::Error;
use hyper::StatusCode;
use std::io;
2020-04-24 10:10:01 +00:00
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)]
2020-04-06 17:55:39 +00:00
HTTP(#[error(source)] http::Error),
#[error(display = "Invalid HTTP header value: {}", _0)]
HTTPHeader(#[error(source)] http::header::ToStrError),
2020-04-12 13:51:19 +00:00
#[error(display = "TLS error: {}", _0)]
TLS(#[error(source)] rustls::TLSError),
#[error(display = "PKI error: {}", _0)]
PKI(#[error(source)] webpki::Error),
2020-04-08 21:01:49 +00:00
#[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),
2020-04-09 16:43:53 +00:00
#[error(display = "JSON error: {}", _0)]
JSON(#[error(source)] serde_json::error::Error),
#[error(display = "TOML decode error: {}", _0)]
2020-04-06 17:55:39 +00:00
TomlDecode(#[error(source)] toml::de::Error),
#[error(display = "Timeout: {}", _0)]
2020-04-06 17:55:39 +00:00
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),
2020-04-06 17:55:39 +00:00
2020-04-09 15:32:28 +00:00
#[error(display = "Bad request: {}", _0)]
BadRequest(String),
2020-04-09 15:32:28 +00:00
#[error(display = "Not found")]
NotFound,
#[error(display = "Corrupt data: does not match hash {:?}", _0)]
CorruptData(Hash),
#[error(display = "{}", _0)]
2020-04-06 17:55:39 +00:00
Message(String),
}
2020-04-09 15:32:28 +00:00
impl Error {
pub fn http_status_code(&self) -> StatusCode {
match self {
Error::BadRequest(_) => StatusCode::BAD_REQUEST,
Error::NotFound => StatusCode::NOT_FOUND,
2020-04-23 16:23:06 +00:00
Error::RPC(_) => StatusCode::SERVICE_UNAVAILABLE,
2020-04-09 15:32:28 +00:00
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
2020-04-09 18:58:39 +00:00
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"))
}
}