hoc/src/error.rs

57 lines
1.3 KiB
Rust
Raw Normal View History

2019-04-19 15:59:51 +02:00
use actix_web::{HttpResponse, ResponseError};
#[derive(Debug)]
pub(crate) enum Error {
2019-04-19 22:11:03 +02:00
Badge(String),
2019-04-19 15:59:51 +02:00
Git(git2::Error),
2019-04-19 22:11:03 +02:00
Internal,
2019-04-19 15:59:51 +02:00
Io(std::io::Error),
ParseColor,
2019-04-19 22:11:03 +02:00
Serial(serde_json::Error),
2019-04-19 15:59:51 +02:00
}
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
2019-04-19 22:11:03 +02:00
Error::Badge(s) => write!(fmt, "Badge({})", s),
2019-04-19 15:59:51 +02:00
Error::Git(e) => write!(fmt, "Git({})", e),
2019-04-19 22:11:03 +02:00
Error::Internal => write!(fmt, "Internal Error"),
2019-04-19 15:59:51 +02:00
Error::Io(e) => write!(fmt, "Io({})", e),
Error::ParseColor => write!(fmt, "Parse error"),
2019-04-19 22:11:03 +02:00
Error::Serial(e) => write!(fmt, "Serial({})", e),
2019-04-19 15:59:51 +02:00
}
}
}
impl ResponseError for Error {
fn error_response(&self) -> HttpResponse {
HttpResponse::InternalServerError().finish()
}
}
impl std::error::Error for Error {}
impl From<String> for Error {
fn from(s: String) -> Self {
Error::Badge(s)
}
}
impl From<git2::Error> for Error {
fn from(err: git2::Error) -> Self {
Error::Git(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
2019-04-19 22:11:03 +02:00
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Serial(err)
}
}