From 1584f445118836c46c1d8c9212506e3fc9a172e1 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Fri, 19 Apr 2019 15:59:51 +0200 Subject: [PATCH] Move errors in own module --- src/error.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..df5bf64 --- /dev/null +++ b/src/error.rs @@ -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 for Error { + fn from(s: String) -> Self { + Error::Badge(s) + } +} + +impl From for Error { + fn from(err: git2::Error) -> Self { + Error::Git(err) + } +} + +impl From for Error { + fn from(err: std::io::Error) -> Self { + Error::Io(err) + } +}