| 1 |
use actix_web::{HttpResponse, ResponseError, http::StatusCode}; |
| 2 |
|
| 3 |
#[derive(Debug, thiserror::Error)] |
| 4 |
pub enum Error { |
| 5 |
#[error("operation failed")] |
| 6 |
Failed, |
| 7 |
|
| 8 |
#[error("{0}")] |
| 9 |
NotFound(String), |
| 10 |
|
| 11 |
#[error(transparent)] |
| 12 |
Render(#[from] askama::Error), |
| 13 |
|
| 14 |
#[error(transparent)] |
| 15 |
Anyhow(#[from] anyhow::Error), |
| 16 |
|
| 17 |
#[error(transparent)] |
| 18 |
Utf8(#[from] std::str::Utf8Error), |
| 19 |
|
| 20 |
#[error(transparent)] |
| 21 |
Format(#[from] std::fmt::Error), |
| 22 |
|
| 23 |
#[error(transparent)] |
| 24 |
Open(#[from] Box<gix::open::Error>), |
| 25 |
|
| 26 |
#[error(transparent)] |
| 27 |
ObjectFind(#[from] gix::object::find::existing::Error), |
| 28 |
|
| 29 |
#[error(transparent)] |
| 30 |
ObjectFindConversion(#[from] gix::object::find::existing::with_conversion::Error), |
| 31 |
|
| 32 |
#[error(transparent)] |
| 33 |
ObjectCommit(#[from] gix::object::commit::Error), |
| 34 |
|
| 35 |
#[error(transparent)] |
| 36 |
ObjectTree(#[from] gix::object::tree::diff::for_each::Error), |
| 37 |
|
| 38 |
#[error(transparent)] |
| 39 |
DiffOptions(#[from] gix::diff::options::init::Error), |
| 40 |
|
| 41 |
#[error(transparent)] |
| 42 |
DiffPrepare(#[from] gix::diff::blob::platform::prepare_diff::Error), |
| 43 |
|
| 44 |
#[error(transparent)] |
| 45 |
DiffResource(#[from] gix::diff::blob::platform::set_resource::Error), |
| 46 |
|
| 47 |
#[error(transparent)] |
| 48 |
RepoDiff(#[from] gix::repository::diff_resource_cache::Error), |
| 49 |
|
| 50 |
#[error(transparent)] |
| 51 |
DecodeCommit(#[from] gix::objs::decode::Error), |
| 52 |
|
| 53 |
#[error(transparent)] |
| 54 |
DecodeHash(#[from] gix::hash::decode::Error), |
| 55 |
|
| 56 |
#[error(transparent)] |
| 57 |
RevWalk(#[from] gix::revision::walk::Error), |
| 58 |
|
| 59 |
#[error(transparent)] |
| 60 |
RevWalkIter(#[from] gix::revision::walk::iter::Error), |
| 61 |
|
| 62 |
#[error(transparent)] |
| 63 |
RevParseSpec(#[from] gix::revision::spec::parse::single::Error), |
| 64 |
|
| 65 |
#[error(transparent)] |
| 66 |
PeelToCommit(#[from] gix::head::peel::to_commit::Error), |
| 67 |
|
| 68 |
#[error(transparent)] |
| 69 |
PeelToKind(#[from] gix::reference::peel::to_kind::Error), |
| 70 |
|
| 71 |
#[error(transparent)] |
| 72 |
ReferenceFind(#[from] gix::reference::find::existing::Error), |
| 73 |
|
| 74 |
#[error(transparent)] |
| 75 |
ReferenceIter(#[from] gix::reference::iter::Error), |
| 76 |
|
| 77 |
#[error(transparent)] |
| 78 |
ReferenceIterInit(#[from] gix::reference::iter::init::Error), |
| 79 |
|
| 80 |
#[error(transparent)] |
| 81 |
Boxed(#[from] Box<dyn std::error::Error + Send + Sync>), |
| 82 |
} |
| 83 |
|
| 84 |
impl ResponseError for Error { |
| 85 |
fn status_code(&self) -> StatusCode { |
| 86 |
match self { |
| 87 |
Self::NotFound(_) => StatusCode::NOT_FOUND, |
| 88 |
_ => StatusCode::INTERNAL_SERVER_ERROR, |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
fn error_response(&self) -> HttpResponse { |
| 93 |
HttpResponse::build(self.status_code()).body(self.to_string()) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
pub fn render_html<T: askama::Template>(template: &T) -> Result<HttpResponse, Error> { |
| 98 |
let html = template.render()?; |
| 99 |
Ok(HttpResponse::Ok().content_type("text/html").body(html)) |
| 100 |
} |