Compare commits

..

3 Commits

Author SHA1 Message Date
1cf1bfcfbc Add Diesel error variant
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 5s
Push Workflows / test (push) Successful in 17s
Push Workflows / docs (push) Successful in 18s
Push Workflows / clippy (push) Successful in 20s
Push Workflows / build (push) Successful in 52s
Push Workflows / nix-build (push) Successful in 4m50s
2026-06-21 12:28:01 -04:00
ff2aba4ec4 Allow contextualizing Option into Result 2026-06-21 12:27:47 -04:00
557489c5ed Add generic error variant 2026-06-21 12:27:20 -04:00

View File

@@ -233,6 +233,13 @@ impl<T, E: Into<Error>> Contextualize<Result<T, Error>> for Result<T, E> {
} }
} }
impl<T> Contextualize<Result<T, Error>> for Option<T> {
#[track_caller]
fn err_context(self, context: impl Into<String>) -> Result<T, Error> {
self.ok_or(Error::new_here(ErrorType::Error(context.into())))
}
}
impl<E: Into<Error>> Contextualize<Error> for E { impl<E: Into<Error>> Contextualize<Error> for E {
#[track_caller] #[track_caller]
fn err_context(self, context: impl Into<String>) -> Error { fn err_context(self, context: impl Into<String>) -> Error {
@@ -242,6 +249,13 @@ impl<E: Into<Error>> Contextualize<Error> for E {
#[derive(Debug, Clone, thiserror::Error, Deserialize, Serialize)] #[derive(Debug, Clone, thiserror::Error, Deserialize, Serialize)]
pub enum ErrorType { pub enum ErrorType {
// Using string to represent Diesel errors, because Diesel's Error type is not `Serialize`,
// and Diesel is only available on the server
#[error("Database error: {0}")]
Database(String),
#[error("{0}")]
Error(String),
} }
impl From<ErrorType> for Error { impl From<ErrorType> for Error {
@@ -250,3 +264,11 @@ impl From<ErrorType> for Error {
Error::new_here(err) Error::new_here(err)
} }
} }
#[cfg(feature = "server")]
impl From<diesel::result::Error> for Error {
#[track_caller]
fn from(err: diesel::result::Error) -> Self {
Error::new_here(ErrorType::Database(format!("{err}")))
}
}