2020-06-02 08:16:23 +00:00
|
|
|
//! Consolidates Diesel and validation errors under a common error type
|
|
|
|
|
2020-04-20 08:44:29 +00:00
|
|
|
extern crate diesel;
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2020-06-16 01:00:32 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-04-20 08:44:29 +00:00
|
|
|
pub enum Error {
|
2020-05-10 16:28:11 +00:00
|
|
|
// The player has already reached the max character limit
|
2020-04-20 08:44:29 +00:00
|
|
|
CharacterLimitReached,
|
2020-08-25 12:21:25 +00:00
|
|
|
// An error occurred while establish a db connection
|
2020-06-16 01:00:32 +00:00
|
|
|
DatabaseConnectionError(diesel::ConnectionError),
|
2020-08-25 12:21:25 +00:00
|
|
|
// An error occurred while running migrations
|
2020-06-16 01:00:32 +00:00
|
|
|
DatabaseMigrationError(diesel_migrations::RunMigrationsError),
|
2020-08-25 12:21:25 +00:00
|
|
|
// An error occurred when performing a database action
|
2020-04-20 08:44:29 +00:00
|
|
|
DatabaseError(diesel::result::Error),
|
2020-05-11 10:06:53 +00:00
|
|
|
// Unable to load body or stats for a character
|
|
|
|
CharacterDataError,
|
2020-04-20 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", match self {
|
|
|
|
Self::CharacterLimitReached => String::from("Character limit exceeded"),
|
2020-06-16 01:00:32 +00:00
|
|
|
Self::DatabaseError(error) => error.to_string(),
|
|
|
|
Self::DatabaseConnectionError(error) => error.to_string(),
|
|
|
|
Self::DatabaseMigrationError(error) => error.to_string(),
|
2020-05-11 10:06:53 +00:00
|
|
|
Self::CharacterDataError => String::from("Error while loading character data"),
|
2020-04-20 08:44:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<diesel::result::Error> for Error {
|
|
|
|
fn from(error: diesel::result::Error) -> Error { Error::DatabaseError(error) }
|
|
|
|
}
|
2020-06-16 01:00:32 +00:00
|
|
|
|
|
|
|
impl From<diesel::ConnectionError> for Error {
|
|
|
|
fn from(error: diesel::ConnectionError) -> Error { Error::DatabaseConnectionError(error) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<diesel_migrations::RunMigrationsError> for Error {
|
|
|
|
fn from(error: diesel_migrations::RunMigrationsError) -> Error {
|
|
|
|
Error::DatabaseMigrationError(error)
|
|
|
|
}
|
|
|
|
}
|