pub enum DatabaseError {
SqlxError(Error),
InvalidData(String),
}Expand description
A generic Error type to be used for functions that perform DB operations.
Note regarding sqlx Errors: Most sqlx errors are of technical nature (like ConnectionPool exhausted, connection unexpectedly closed, timeouts). These are likely not handled specifically by calling code, and mostly just cause the operation to fail.
Some sqlx errors (such as ConstraintViolation) may actually be useful for the caller. For example, in a signup process, we may very well want to handle the case where an email address is already registered. In this case, the recommended pattern is to create a new error type containing the errors you want the caller to handle explicitly, and include a variant that holds the DatabaseError. This makes it very clear to the caller what application-level conditions may occur.
Example:
#[derive(thiserror::Error, Debug)]
enum SignupError {
// This typically comes from a sqlx constraint violation, but we want
// the caller to handle this explicitly.
#[error("username already taken")]
UsernameAlreadyTaken,
// All other "uninteresting" sqlx errors will be signaled via the DatabaseError.
#[error(transparent)]
DatabaseError(arminius_lib_db::DatabaseError),
}Trivial query functions (like simple SELECT queries) that do not have any non-technical error cases may of course return this type directly.
Variants§
SqlxError(Error)
An sqlx error.
InvalidData(String)
Unexpected content read from the database. E.g. a JSON field failed to deserialize, a field did not conform to a specific format (like an email address) etc
Trait Implementations§
Source§impl Debug for DatabaseError
impl Debug for DatabaseError
Source§impl Display for DatabaseError
impl Display for DatabaseError
Source§impl Error for DatabaseError
impl Error for DatabaseError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Auto Trait Implementations§
impl !RefUnwindSafe for DatabaseError
impl !UnwindSafe for DatabaseError
impl Freeze for DatabaseError
impl Send for DatabaseError
impl Sync for DatabaseError
impl Unpin for DatabaseError
impl UnsafeUnpin for DatabaseError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more