veloren_server/persistence/
error.rs

1//! Consolidates rusqlite and validation errors under a common error type
2
3extern crate rusqlite;
4
5use std::fmt;
6
7#[derive(Debug)]
8pub enum PersistenceError {
9    // An invalid asset was returned from the database
10    AssetError(String),
11    // The player has already reached the max character limit
12    CharacterLimitReached,
13    // An error occurred while establish a db connection
14    DatabaseConnectionError(rusqlite::Error),
15    // An error occurred when performing a database action
16    DatabaseError(rusqlite::Error),
17    // Unable to load body or stats for a character
18    CharacterDataError,
19    SerializationError(serde_json::Error),
20    ConversionError(String),
21    OtherError(String),
22}
23
24impl fmt::Display for PersistenceError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "{}", match self {
27            Self::AssetError(error) => error.to_string(),
28            Self::CharacterLimitReached => String::from("Character limit exceeded"),
29            Self::DatabaseError(error) => error.to_string(),
30            Self::DatabaseConnectionError(error) => error.to_string(),
31            Self::CharacterDataError => String::from("Error while loading character data"),
32            Self::SerializationError(error) => error.to_string(),
33            Self::ConversionError(error) => error.to_string(),
34            Self::OtherError(error) => error.to_string(),
35        })
36    }
37}
38
39impl From<rusqlite::Error> for PersistenceError {
40    fn from(error: rusqlite::Error) -> PersistenceError { PersistenceError::DatabaseError(error) }
41}
42
43impl From<serde_json::Error> for PersistenceError {
44    fn from(error: serde_json::Error) -> PersistenceError {
45        PersistenceError::SerializationError(error)
46    }
47}