bin/src/error.rs

55 lines
1.6 KiB
Rust
Raw Normal View History

2023-10-20 00:40:24 +02:00
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
2023-10-18 16:14:44 +02:00
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,
2023-10-20 00:40:24 +02:00
#[error("oidc redirect")]
Oidc(Response),
2023-10-18 16:14:44 +02:00
}
impl IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
match self {
2023-10-20 00:40:24 +02:00
Self::PhraseInvalid => (StatusCode::BAD_REQUEST, "url is not valid\n").into_response(),
Self::BinNotFound => (StatusCode::NOT_FOUND, "bin does not exist\n").into_response(),
Self::DataFileExists => {
(StatusCode::CONFLICT, "bin already contains data\n").into_response()
}
Self::ParseTtl => (StatusCode::BAD_REQUEST, "invalid ttl class\n").into_response(),
Self::Oidc(response) => response.into_response(),
2023-10-18 16:14:44 +02:00
_ => {
error!("{:?}", self);
2023-10-20 00:40:24 +02:00
(StatusCode::INTERNAL_SERVER_ERROR, "internal server error\n").into_response()
2023-10-18 16:14:44 +02:00
}
}
.into_response()
}
}