2024-05-11 22:47:59 +02:00
|
|
|
use axum::{http::StatusCode, response::Redirect};
|
2024-04-23 04:07:32 +02:00
|
|
|
|
2024-03-27 04:00:18 +01:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum UpubError {
|
2024-05-13 14:56:16 +02:00
|
|
|
#[error("database error: {0:?}")]
|
2024-03-27 04:00:18 +01:00
|
|
|
Database(#[from] sea_orm::DbErr),
|
|
|
|
|
2024-04-23 04:07:32 +02:00
|
|
|
#[error("{0}")]
|
2024-03-27 04:00:18 +01:00
|
|
|
Status(axum::http::StatusCode),
|
|
|
|
|
|
|
|
#[error("missing field: {0}")]
|
|
|
|
Field(#[from] crate::model::FieldError),
|
|
|
|
|
2024-05-13 14:56:16 +02:00
|
|
|
#[error("openssl error: {0:?}")]
|
2024-03-27 04:00:18 +01:00
|
|
|
OpenSSL(#[from] openssl::error::ErrorStack),
|
|
|
|
|
2024-05-13 14:56:16 +02:00
|
|
|
#[error("invalid UTF8 in key: {0:?}")]
|
2024-04-09 01:14:48 +02:00
|
|
|
OpenSSLParse(#[from] std::str::Utf8Error),
|
|
|
|
|
2024-05-13 14:56:16 +02:00
|
|
|
#[error("fetch error: {0:?}")]
|
2024-03-27 04:00:18 +01:00
|
|
|
Reqwest(#[from] reqwest::Error),
|
2024-04-13 06:02:50 +02:00
|
|
|
|
2024-05-11 17:08:38 +02:00
|
|
|
// TODO this is quite ugly because its basically a reqwest::Error but with extra string... buuut
|
|
|
|
// helps with debugging!
|
2024-05-13 14:56:16 +02:00
|
|
|
#[error("fetch error: {0:?} -- server responded with {1}")]
|
2024-05-11 17:08:38 +02:00
|
|
|
FetchError(reqwest::Error, String),
|
|
|
|
|
2024-05-13 14:56:16 +02:00
|
|
|
#[error("invalid base64 string: {0:?}")]
|
2024-04-13 06:02:50 +02:00
|
|
|
Base64(#[from] base64::DecodeError),
|
2024-04-29 20:11:19 +02:00
|
|
|
|
|
|
|
// TODO this isn't really an error but i need to redirect from some routes so this allows me to
|
|
|
|
// keep the type hints on the return type, still what the hell!!!!
|
|
|
|
#[error("redirecting to {0}")]
|
|
|
|
Redirect(String),
|
2024-03-27 04:00:18 +01:00
|
|
|
}
|
|
|
|
|
2024-03-28 04:52:17 +01:00
|
|
|
impl UpubError {
|
2024-04-08 02:30:43 +02:00
|
|
|
pub fn bad_request() -> Self {
|
2024-04-08 02:52:51 +02:00
|
|
|
Self::Status(axum::http::StatusCode::BAD_REQUEST)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unprocessable() -> Self {
|
|
|
|
Self::Status(axum::http::StatusCode::UNPROCESSABLE_ENTITY)
|
2024-03-28 04:52:17 +01:00
|
|
|
}
|
2024-04-09 01:14:48 +02:00
|
|
|
|
|
|
|
pub fn not_found() -> Self {
|
|
|
|
Self::Status(axum::http::StatusCode::NOT_FOUND)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn forbidden() -> Self {
|
|
|
|
Self::Status(axum::http::StatusCode::FORBIDDEN)
|
|
|
|
}
|
|
|
|
|
2024-04-13 01:49:04 +02:00
|
|
|
pub fn unauthorized() -> Self {
|
|
|
|
Self::Status(axum::http::StatusCode::UNAUTHORIZED)
|
|
|
|
}
|
|
|
|
|
2024-04-09 01:14:48 +02:00
|
|
|
pub fn not_modified() -> Self {
|
|
|
|
Self::Status(axum::http::StatusCode::NOT_MODIFIED)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn internal_server_error() -> Self {
|
|
|
|
Self::Status(axum::http::StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
}
|
2024-03-28 04:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type UpubResult<T> = Result<T, UpubError>;
|
|
|
|
|
2024-03-27 04:00:18 +01:00
|
|
|
impl From<axum::http::StatusCode> for UpubError {
|
|
|
|
fn from(value: axum::http::StatusCode) -> Self {
|
|
|
|
UpubError::Status(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl axum::response::IntoResponse for UpubError {
|
|
|
|
fn into_response(self) -> axum::response::Response {
|
2024-04-13 04:08:45 +02:00
|
|
|
let descr = self.to_string();
|
|
|
|
match self {
|
2024-04-29 20:11:19 +02:00
|
|
|
UpubError::Redirect(to) => Redirect::to(&to).into_response(),
|
2024-04-13 04:08:45 +02:00
|
|
|
UpubError::Status(status) => (status, descr).into_response(),
|
2024-04-23 04:07:32 +02:00
|
|
|
UpubError::Database(_) => (StatusCode::SERVICE_UNAVAILABLE, descr).into_response(),
|
2024-05-11 22:47:59 +02:00
|
|
|
UpubError::Reqwest(x) | UpubError::FetchError(x, _) =>
|
2024-04-23 04:07:32 +02:00
|
|
|
(
|
|
|
|
x.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
|
|
|
|
format!("failed fetching '{}': {descr}", x.url().map(|x| x.to_string()).unwrap_or_default())
|
|
|
|
).into_response(),
|
|
|
|
UpubError::Field(_) => (axum::http::StatusCode::BAD_REQUEST, descr).into_response(),
|
|
|
|
_ => (StatusCode::INTERNAL_SERVER_ERROR, descr).into_response(),
|
2024-04-13 04:08:45 +02:00
|
|
|
}
|
2024-03-27 04:00:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 17:43:30 +01:00
|
|
|
pub trait LoggableError {
|
|
|
|
fn info_failed(self, msg: &str);
|
|
|
|
fn warn_failed(self, msg: &str);
|
|
|
|
fn err_failed(self, msg: &str);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E: std::error::Error> LoggableError for Result<T, E> {
|
|
|
|
fn info_failed(self, msg: &str) {
|
|
|
|
if let Err(e) = self {
|
|
|
|
tracing::info!("{} : {}", msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn warn_failed(self, msg: &str) {
|
|
|
|
if let Err(e) = self {
|
|
|
|
tracing::warn!("{} : {}", msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn err_failed(self, msg: &str) {
|
|
|
|
if let Err(e) = self {
|
|
|
|
tracing::error!("{} : {}", msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|