bin/src/error.rs

46 lines
1.4 KiB
Rust
Raw Normal View History

2023-10-18 16:14:44 +02:00
use axum::{http::StatusCode, response::IntoResponse};
use log::error;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("io error: {:?}", 0)]
Io(#[from] std::io::Error),
#[error("time error: {:?}", 0)]
Time(#[from] std::time::SystemTimeError),
#[error("metadata error: {:?}", 0)]
MetadataDe(#[from] toml::de::Error),
#[error("metadata error: {:?}", 0)]
MetadataSer(#[from] toml::ser::Error),
#[error("phrase is not valid")]
PhraseInvalid,
#[error("bin could not be found")]
BinNotFound,
#[error("file exists")]
DataFileExists,
#[error("hex error: {:?}", 0)]
Hex(#[from] hex::FromHexError),
#[error("could not parse ttl")]
ParseTtl,
#[error("encryption error")]
ChaCha,
}
impl IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
match self {
Self::PhraseInvalid => (StatusCode::BAD_REQUEST, "url is not valid\n"),
Self::BinNotFound => (StatusCode::NOT_FOUND, "bin does not exist\n"),
Self::DataFileExists => (StatusCode::CONFLICT, "bin already contains data\n"),
Self::ParseTtl => (StatusCode::BAD_REQUEST, "invalid ttl class\n"),
2023-10-18 16:14:44 +02:00
_ => {
error!("{:?}", self);
(StatusCode::INTERNAL_SERVER_ERROR, "internal server error\n")
2023-10-18 16:14:44 +02:00
}
}
.into_response()
}
}