Compare commits
3 Commits
aba4556144
...
1cf1bfcfbc
| Author | SHA1 | Date | |
|---|---|---|---|
|
1cf1bfcfbc
|
|||
|
ff2aba4ec4
|
|||
|
557489c5ed
|
@@ -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}")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user