Move errors in own module

This commit is contained in:
Valentin Brandl 2019-04-19 15:59:51 +02:00
parent 48ce41e5fd
commit 1584f44511
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

46
src/error.rs Normal file
View File

@ -0,0 +1,46 @@
use actix_web::{HttpResponse, ResponseError};
#[derive(Debug)]
pub(crate) enum Error {
Git(git2::Error),
Io(std::io::Error),
Badge(String),
ParseColor,
}
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::Git(e) => write!(fmt, "Git({})", e),
Error::Io(e) => write!(fmt, "Io({})", e),
Error::Badge(s) => write!(fmt, "Badge({})", s),
Error::ParseColor => write!(fmt, "Parse error"),
}
}
}
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)
}
}