webdav_server/model/
error.rs1use std::{num::TryFromIntError, time::SystemTimeError};
2
3use axum::response::IntoResponse;
4use hyper::StatusCode;
5
6use crate::{
7 error::internal,
8 logger::{Level, Loggable, Module},
9 wrap_internal_err,
10};
11
12#[derive(thiserror::Error, Debug)]
18pub enum Error {
19 #[error("Database Error : {}", .0)]
22 Database(#[from] sqlx::Error),
23
24 #[error("Asset not found")]
30 AssetNotFound,
31
32 #[error("Internal Err {}", .0)]
34 Internal(#[from] internal::Error),
35}
36
37pub type Result<T> = std::result::Result<T, Error>;
38
39wrap_internal_err! {
40 TryFromIntError, SystemTimeError => Error::Internal
41}
42
43impl IntoResponse for Error {
44 fn into_response(self) -> axum::response::Response {
45 use Error::{AssetNotFound, Database, Internal};
46
47 match self {
48 AssetNotFound => {
50 (StatusCode::NOT_FOUND, "Asset not found in database")
51 }
52
53 Database(_) | Internal(_) => {
55 (StatusCode::INTERNAL_SERVER_ERROR, "Internal Server Error")
56 }
57 }
58 .into_response()
59 }
60}
61
62impl Loggable for Error {
63 fn log_level(&self) -> crate::logger::Level {
64 use Error::{AssetNotFound, Database, Internal};
65 match self {
66 AssetNotFound => Level::Info,
67 Internal(_) => Level::Fatal,
68 Database(_) => Level::Error,
69 }
70 }
71
72 #[inline]
73 fn log_module(&self) -> crate::logger::Module {
74 Module::Database
75 }
76}