mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 20:32:48 +00:00
57 lines
1.6 KiB
Rust
57 lines
1.6 KiB
Rust
use axum::response::IntoResponse;
|
|
use http::StatusCode;
|
|
use tracing::error;
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error("Unauthorized")]
|
|
Unauthorized,
|
|
|
|
#[error("Not Found")]
|
|
NotFound,
|
|
|
|
#[error("Not implemented")]
|
|
NotImplemented,
|
|
|
|
#[error(transparent)]
|
|
StoreError(#[from] rustical_store::Error),
|
|
|
|
#[error(transparent)]
|
|
ChronoParseError(#[from] chrono::ParseError),
|
|
|
|
#[error(transparent)]
|
|
DavError(#[from] rustical_dav::Error),
|
|
|
|
#[error(transparent)]
|
|
XmlDecodeError(#[from] rustical_xml::XmlError),
|
|
|
|
#[error(transparent)]
|
|
IcalError(#[from] rustical_ical::Error),
|
|
}
|
|
|
|
impl Error {
|
|
pub fn status_code(&self) -> StatusCode {
|
|
match self {
|
|
Error::StoreError(err) => match err {
|
|
rustical_store::Error::NotFound => StatusCode::NOT_FOUND,
|
|
rustical_store::Error::AlreadyExists => StatusCode::CONFLICT,
|
|
rustical_store::Error::ReadOnly => StatusCode::FORBIDDEN,
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
},
|
|
Error::ChronoParseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Error::DavError(err) => err.status_code(),
|
|
Error::Unauthorized => StatusCode::UNAUTHORIZED,
|
|
Error::XmlDecodeError(_) => StatusCode::BAD_REQUEST,
|
|
Error::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Error::NotFound => StatusCode::NOT_FOUND,
|
|
Self::IcalError(err) => err.status_code(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IntoResponse for Error {
|
|
fn into_response(self) -> axum::response::Response {
|
|
(self.status_code(), self.to_string()).into_response()
|
|
}
|
|
}
|