webdav_server/api/
error.rs1use axum::response::IntoResponse;
2use hyper::{StatusCode, header::InvalidHeaderValue};
3use tokio::io;
4
5use crate::{error::internal, logger::Loggable};
6
7#[derive(Debug, thiserror::Error)]
16pub enum Error {
17 #[error("Malformed Multipart found")]
19 MalformedMultipart,
20
21 #[error("Bad Request")]
24 BadRequest(String),
25
26 #[error("Stream Read Error")]
29 StreamReadError,
30
31 #[error("Missing Field")]
33 MissingField,
34
35 #[error("IO Error : {}", .0)]
38 IoError(#[from] io::Error),
39
40 #[error("Unauthorized : {}", .0)]
43 Unauthorized(String),
44
45 #[error("Internal Server Error : {}", .0)]
47 Internal(#[from] internal::Error),
48
49 #[error("Not Found")]
52 NotFound(String),
53
54 #[error("Invalid header value : {}", .0)]
57 InvalidHeader(#[from] InvalidHeaderValue),
58}
59
60pub type Result<T> = core::result::Result<T, Error>;
61
62impl IntoResponse for Error {
63 fn into_response(self) -> axum::response::Response {
64 use Error::{
65 BadRequest, Internal, InvalidHeader, IoError, MalformedMultipart,
66 MissingField, NotFound, StreamReadError, Unauthorized,
67 };
68
69 match self {
70 BadRequest(msg) => (StatusCode::BAD_REQUEST, msg),
72 Unauthorized(msg) => (StatusCode::UNAUTHORIZED, msg),
73 NotFound(msg) => (StatusCode::NOT_FOUND, msg),
74 MalformedMultipart => (
75 StatusCode::BAD_REQUEST,
76 "Malformed Multipart Payload".into(),
77 ),
78 MissingField => {
79 (StatusCode::BAD_REQUEST, "Missing required field".into())
80 }
81 InvalidHeader(_) => {
82 (StatusCode::BAD_REQUEST, "Invalid Header Value".into())
83 }
84
85 StreamReadError | IoError(_) | Internal(_) => (
87 StatusCode::INTERNAL_SERVER_ERROR,
88 "Internal Server Error".into(),
89 ),
90 }
91 .into_response()
92 }
93}
94
95use crate::logger::{Level, Module};
96impl Loggable for Error {
97 fn log_level(&self) -> Level {
98 use Error::{BadRequest, MissingField, NotFound, Unauthorized};
99 match self {
100 BadRequest(_) | Unauthorized(_) | MissingField => Level::Warning,
101 NotFound(_) => Level::Info,
102 _ => Level::Error,
103 }
104 }
105
106 #[inline]
107 fn log_module(&self) -> Module {
108 Module::Api
109 }
110}