use derive_more::{Display, From};
use ntex::http::StatusCode;
use ntex::web::error::{DefaultError, WebResponseError};
#[derive(Display, Debug, From)]
pub enum FilesError {
#[allow(dead_code)]
#[display(fmt = "Path is not a directory. Unable to serve static files.")]
IsNotDirectory,
#[display(fmt = "Unable to render directory without index file.")]
IsDirectory,
#[display(fmt = "Request did not meet this resource's requirements.")]
MethodNotAllowed,
#[display(fmt = "{}", _0)]
Uri(UriSegmentError),
#[display(fmt = "Error reading: {}", _0)]
Io(std::io::Error),
}
impl WebResponseError<DefaultError> for FilesError {
fn status_code(&self) -> StatusCode {
match self {
FilesError::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED,
FilesError::Uri(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::NOT_FOUND,
}
}
}
#[derive(Display, Debug, PartialEq)]
pub enum UriSegmentError {
#[display(fmt = "The segment started with the wrapped invalid character")]
BadStart(char),
#[display(fmt = "The segment contained the wrapped invalid character")]
BadChar(char),
#[display(fmt = "The segment ended with the wrapped invalid character")]
BadEnd(char),
}
impl WebResponseError<DefaultError> for UriSegmentError {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}