fish_lib/game/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::game::errors::database::GameDatabaseError;
use crate::game::errors::resource::GameResourceError;
use thiserror::Error;

pub mod database;
pub mod repository;
pub mod resource;

pub type GameResult<T> = Result<T, GameError>;

#[derive(Error, Debug)]
pub enum GameError {
    #[error(transparent)]
    Database(#[from] GameDatabaseError),
    #[error(transparent)]
    Resource(#[from] GameResourceError),
    #[error("Unexpected error: {msg}")]
    Unexpected {
        msg: String,
        #[source]
        source: Box<dyn std::error::Error>,
    },
}

impl GameError {
    pub fn unexpected(error: Box<dyn std::error::Error>) -> Self {
        Self::Unexpected {
            msg: error.to_string(),
            source: error,
        }
    }

    pub fn as_resource_error(&self) -> Option<&GameResourceError> {
        match self {
            GameError::Resource(e) => Some(e),
            _ => None,
        }
    }
}

impl From<Box<dyn std::error::Error>> for GameError {
    fn from(error: Box<dyn std::error::Error>) -> Self {
        match error {
            e if e.is::<GameError>() => *e.downcast::<GameError>().unwrap(),
            e if e.is::<GameDatabaseError>() => {
                GameError::Database(*e.downcast::<GameDatabaseError>().unwrap())
            }
            e if e.is::<GameResourceError>() => {
                GameError::Resource(*e.downcast::<GameResourceError>().unwrap())
            }
            e => Self::unexpected(e),
        }
    }
}