2021-03-22 12:46:10 +01:00
|
|
|
use crate::{statics::VERSION_INFO, templates};
|
|
|
|
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
|
|
|
use std::fmt;
|
2019-04-19 15:59:51 +02:00
|
|
|
|
2019-05-04 15:33:21 +02:00
|
|
|
pub(crate) type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2019-04-19 15:59:51 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum Error {
|
2019-04-19 22:11:03 +02:00
|
|
|
Badge(String),
|
2019-04-21 20:44:47 +02:00
|
|
|
Client(reqwest::Error),
|
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),
|
2019-07-07 13:29:26 +02:00
|
|
|
Parse(std::num::ParseIntError),
|
2019-04-19 22:11:03 +02:00
|
|
|
Serial(serde_json::Error),
|
2020-07-20 20:14:22 +02:00
|
|
|
BranchNotFound,
|
2019-04-19 15:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 17:57:57 +02:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2019-04-19 15:59:51 +02:00
|
|
|
match self {
|
2019-04-19 22:11:03 +02:00
|
|
|
Error::Badge(s) => write!(fmt, "Badge({})", s),
|
2019-04-21 20:44:47 +02:00
|
|
|
Error::Client(e) => write!(fmt, "Client({})", e),
|
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),
|
2019-07-07 13:29:26 +02:00
|
|
|
Error::Parse(e) => write!(fmt, "Parse({})", e),
|
2019-04-19 22:11:03 +02:00
|
|
|
Error::Serial(e) => write!(fmt, "Serial({})", e),
|
2020-07-20 20:14:22 +02:00
|
|
|
Error::BranchNotFound => write!(fmt, "Repo doesn't have master branch"),
|
2019-04-19 15:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseError for Error {
|
2021-03-22 12:46:10 +01:00
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
match self {
|
|
|
|
Error::BranchNotFound => StatusCode::NOT_FOUND,
|
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 15:59:51 +02:00
|
|
|
fn error_response(&self) -> HttpResponse {
|
2019-06-12 21:51:16 +02:00
|
|
|
let mut buf = Vec::new();
|
2020-02-12 20:56:47 +01:00
|
|
|
match self {
|
2020-07-20 20:14:22 +02:00
|
|
|
Error::BranchNotFound => {
|
2021-03-22 12:46:10 +01:00
|
|
|
templates::p404_no_master(&mut buf, VERSION_INFO, 0).unwrap();
|
2020-03-21 13:02:53 +01:00
|
|
|
HttpResponse::NotFound().content_type("text/html").body(buf)
|
|
|
|
}
|
2020-02-12 20:56:47 +01:00
|
|
|
_ => {
|
2021-03-22 12:46:10 +01:00
|
|
|
templates::p500(&mut buf, VERSION_INFO, 0).unwrap();
|
2020-02-12 20:56:47 +01:00
|
|
|
HttpResponse::InternalServerError()
|
|
|
|
.content_type("text/html")
|
|
|
|
.body(buf)
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 15:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-04-21 20:44:47 +02:00
|
|
|
|
|
|
|
impl From<reqwest::Error> for Error {
|
|
|
|
fn from(err: reqwest::Error) -> Self {
|
|
|
|
Error::Client(err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 01:11:54 +02:00
|
|
|
|
2019-07-07 13:29:26 +02:00
|
|
|
impl From<std::num::ParseIntError> for Error {
|
|
|
|
fn from(err: std::num::ParseIntError) -> Self {
|
|
|
|
Error::Parse(err)
|
|
|
|
}
|
|
|
|
}
|