jae-blog/src/error.rs

48 lines
1.4 KiB
Rust
Raw Normal View History

2024-04-18 04:05:38 +03:00
use std::fmt::Display;
use axum::{http::StatusCode, response::IntoResponse};
use thiserror::Error;
// fronma is too lazy to implement std::error::Error for their own types
#[derive(Debug)]
#[repr(transparent)]
2024-04-19 16:46:12 +03:00
pub struct FronmaError(fronma::error::Error);
2024-04-18 04:05:38 +03:00
2024-04-19 16:46:12 +03:00
impl std::error::Error for FronmaError {}
2024-04-18 04:05:38 +03:00
2024-04-19 16:46:12 +03:00
impl Display for FronmaError {
2024-04-18 04:05:38 +03:00
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("failed to parse front matter: ")?;
match &self.0 {
fronma::error::Error::MissingBeginningLine => f.write_str("missing beginning line"),
fronma::error::Error::MissingEndingLine => f.write_str("missing ending line"),
2024-04-18 04:12:03 +03:00
fronma::error::Error::SerdeYaml(yaml_error) => write!(f, "{}", yaml_error),
2024-04-18 04:05:38 +03:00
}
}
}
#[derive(Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum PostError {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
AskamaError(#[from] askama::Error),
#[error(transparent)]
2024-04-19 16:46:12 +03:00
ParseError(#[from] FronmaError),
2024-04-18 04:05:38 +03:00
#[error("post {0:?} not found")]
NotFound(String),
}
impl From<fronma::error::Error> for PostError {
fn from(value: fronma::error::Error) -> Self {
2024-04-19 16:46:12 +03:00
Self::ParseError(FronmaError(value))
2024-04-18 04:05:38 +03:00
}
}
impl IntoResponse for PostError {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
}
}