use axum::{
http::{Error as HttpError, StatusCode},
response::{IntoResponse, Response},
};
use mime_guess::Mime;
use rubedo::sugar::s;
use std::{
io::Error as IoError,
path::PathBuf,
};
use thiserror::Error as ThisError;
#[derive(Debug, ThisError)]
#[non_exhaustive]
pub enum AssetsError {
#[error("Failed to build a response body: {0}")]
FailedToBuildResponseBody(HttpError),
#[error("Failed to get metadata for local file {0}: {1}")]
FailedToGetLocalFileMetadata(PathBuf, IoError),
#[error("Failed to open local file {0}: {1}")]
FailedToOpenLocalFile(PathBuf, IoError),
#[error("Failed to read local file {0}: {1}")]
FailedToReadLocalFile(PathBuf, IoError),
#[error("Invalid MIME type header: {0}")]
InvalidMimeTypeHeader(Mime),
#[error("Local file not found: {0}")]
LocalFileNotFound(PathBuf),
#[error("Packaged file not found: {0}")]
PackagedFileNotFound(String),
}
impl IntoResponse for AssetsError {
fn into_response(self) -> Response {
match self {
Self::LocalFileNotFound(_) |
Self::PackagedFileNotFound(_) => (StatusCode::NOT_FOUND, s!("")),
Self::FailedToBuildResponseBody(_) |
Self::FailedToGetLocalFileMetadata(..) |
Self::FailedToOpenLocalFile(..) |
Self::FailedToReadLocalFile(..) |
Self::InvalidMimeTypeHeader(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
}.into_response()
}
}