use std::*;
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("That is not a valid email address.")]
InvalidEmailAddressError,
#[cfg(feature = "sqlx-sqlite")]
#[error("The mutex guarding the Sqlite connection was posioned.")]
MutexPoisonError,
#[error("Could not find any user that fits the specified requirements.")]
UserNotFoundError,
#[error("UnmanagedStateError: failed retrieving `Users`. You may be missing `.manage(users)` in your app.")]
UnmanagedStateError,
#[error("UnauthenticatedError: The operation failed because the client is not authenticated.")]
UnauthenticatedError,
#[error("The email \"{0}\" is not registered. Try signing up first.")]
EmailDoesNotExist(String),
#[error("That email address already exists. Try logging in.")]
EmailAlreadyExists,
#[error("Incorrect email or password")]
UnauthorizedError,
#[error("{0}")]
FormValidationError(#[from] validator::ValidationError),
#[error("FormValidationErrors: {0}")]
FormValidationErrors(#[from] validator::ValidationErrors),
#[cfg(any(feature = "sqlx"))]
#[error("SqlxError: {0}")]
SqlxError(#[from] sqlx::Error),
#[error("Argon2ParsingError: {0}")]
Argon2ParsingError(#[from] argon2::Error),
#[cfg(feature = "rusqlite")]
#[error("RusqliteError: {0}")]
RusqliteError(#[from] rusqlite::Error),
#[cfg(feature = "redis")]
#[error("RedisError")]
RedisError(#[from] redis::RedisError),
#[error("SerdeError: {0}")]
SerdeError(#[from] serde_json::Error),
#[cfg(feature = "sqlx-postgres")]
#[error("IOError: {0}")]
IOError(#[from] std::io::Error),
#[cfg(feature = "tokio-postgres")]
#[error("TokioPostgresError: {0}")]
TokioPostgresError(#[from] tokio_postgres::Error),
}
#[cfg(feature = "sqlx-sqlite")]
use std::sync::PoisonError;
#[cfg(feature = "sqlx-sqlite")]
impl<T> From<PoisonError<T>> for Error {
fn from(_error: PoisonError<T>) -> Error {
Error::MutexPoisonError
}
}
use self::Error::*;
impl Error {
fn message(&self) -> String {
match self {
InvalidEmailAddressError
| EmailAlreadyExists
| UnauthorizedError
| UserNotFoundError => format!("{}", self),
FormValidationErrors(source) => {
source
.field_errors()
.into_iter()
.map(|(_, error)| error)
.map(IntoIterator::into_iter)
.map(|errs| {
errs .map(|err| &err.code)
.fold(String::new(), |a, b| a + b)
})
.fold(String::new(), |a, b| a + &b)
}
#[cfg(debug_assertions)]
e => return format!("{}", e),
#[allow(unreachable_patterns)]
_ => "undefined".into(),
}
}
}
use rocket::http::ContentType;
use rocket::request::Request;
use rocket::response::{self, Responder, Response};
use serde_json::*;
use std::io::Cursor;
impl<'r> Responder<'r, 'static> for Error {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
let payload = to_string(&json!({
"status": "error",
"message": self.message(),
}))
.unwrap();
Response::build()
.sized_body(payload.len(), Cursor::new(payload))
.header(ContentType::new("application", "json"))
.ok()
}
}