2023-11-11 16:27:40 +01:00
|
|
|
use std::error::Error;
|
|
|
|
|
use std::fmt::{Debug, Display, Formatter};
|
2023-12-08 17:02:00 +01:00
|
|
|
use leptos::ServerFnError;
|
2023-11-11 16:27:40 +01:00
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
|
pub enum AppError {
|
2023-12-08 17:02:00 +01:00
|
|
|
HourParseError,
|
|
|
|
|
ServerError(String),
|
|
|
|
|
FatalError(String)
|
2023-11-11 16:27:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppError {
|
|
|
|
|
fn as_string(&self) -> String {
|
2023-12-08 17:02:00 +01:00
|
|
|
match self {
|
|
|
|
|
AppError::HourParseError => {"Hour parse error".to_string()},
|
|
|
|
|
AppError::ServerError(e) => {format!("Server error: {}", e)},
|
|
|
|
|
AppError::FatalError(e) => {format!("Fatal error: {}", e)}
|
|
|
|
|
}
|
2023-11-11 16:27:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for AppError {
|
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
write!(f, "{}", self.as_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 17:02:00 +01:00
|
|
|
impl Error for AppError {}
|
|
|
|
|
|
|
|
|
|
impl From<ServerFnError> for AppError {
|
|
|
|
|
fn from(value: ServerFnError) -> Self {
|
|
|
|
|
AppError::ServerError(value.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ssr")]
|
|
|
|
|
impl From<sqlx::Error> for AppError {
|
|
|
|
|
fn from(value: sqlx::Error) -> Self {
|
|
|
|
|
AppError::FatalError(value.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|