good_web_game/
error.rs

1//! Error types and conversion functions.
2
3use std::error::Error;
4use std::fmt;
5
6/// An enum containing all kinds of game framework errors.
7#[derive(Debug)]
8pub enum GameError {
9    /// Something went wrong trying to read from a file
10    IOError(std::io::Error),
11    /// Something went wrong compiling shaders
12    ShaderProgramError(String),
13    /// Something went wrong with the `gilrs` gamepad-input library.
14    GamepadError(String),
15    /// Something went wrong with the `lyon` shape-tesselation library
16    LyonError(String),
17    /// SoundMixer in the context should be created explicitly from some of the interaction callbacks
18    /// Thats the only way to get audio to works on web :(
19    MixerNotCreated,
20    SoundError,
21    UnknownError(String),
22    /// Unable to find a resource; the `Vec` is the paths it searched for and associated errors
23    ResourceNotFound(String, Vec<(std::path::PathBuf, GameError)>),
24    /// An error in the filesystem layout
25    FilesystemError(String),
26    /// An error trying to load a resource, such as getting an invalid image file.
27    ResourceLoadError(String),
28    /// Something went wrong in the renderer
29    RenderError(String),
30    /// A custom error type for use by users of ggez.
31    /// This lets you handle custom errors that may happen during your game (such as, trying to load a malformed file for a level)
32    /// using the same mechanism you handle ggez's other errors.
33    ///
34    /// Please include an informative message with the error.
35    CustomError(String),
36}
37
38impl fmt::Display for GameError {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        write!(f, "GameError {:?}", self)
41    }
42}
43
44impl Error for GameError {
45    fn cause(&self) -> Option<&dyn Error> {
46        match *self {
47            GameError::IOError(ref e) => Some(e),
48            _ => None,
49        }
50    }
51}
52
53/// A convenient result type consisting of a return type and a `GameError`
54pub type GameResult<T = ()> = Result<T, GameError>;
55
56impl From<std::io::Error> for GameError {
57    fn from(e: std::io::Error) -> GameError {
58        GameError::IOError(e)
59    }
60}
61/*
62impl From<miniquad_text_rusttype::Error> for GameError {
63    fn from(e: miniquad_text_rusttype::Error) -> GameError {
64        GameError::TTFError(e)
65    }
66}
67*/
68#[cfg(feature = "mesh")]
69impl From<lyon::lyon_tessellation::TessellationError> for GameError {
70    fn from(s: lyon::lyon_tessellation::TessellationError) -> GameError {
71        let errstr = format!(
72            "Error while tesselating shape (did you give it an infinity or NaN?): {:?}",
73            s
74        );
75        GameError::LyonError(errstr)
76    }
77}
78
79#[cfg(feature = "mesh")]
80impl From<lyon::lyon_tessellation::geometry_builder::GeometryBuilderError> for GameError {
81    fn from(s: lyon::lyon_tessellation::geometry_builder::GeometryBuilderError) -> GameError {
82        let errstr = format!(
83            "Error while building geometry (did you give it too many vertices?): {:?}",
84            s
85        );
86        GameError::LyonError(errstr)
87    }
88}
89
90impl From<zip::result::ZipError> for GameError {
91    fn from(e: zip::result::ZipError) -> GameError {
92        let errstr = format!("Zip error: {}", e);
93        GameError::ResourceLoadError(errstr)
94    }
95}
96
97#[cfg(not(any(target_arch = "wasm32", target_os = "ios", target_os = "android",)))]
98impl From<gilrs::Error> for GameError {
99    fn from(s: gilrs::Error) -> GameError {
100        let errstr = format!("Gamepad error: {}", s);
101        GameError::GamepadError(errstr)
102    }
103}
104
105impl From<miniquad::ShaderError> for GameError {
106    fn from(e: miniquad::ShaderError) -> GameError {
107        let errstr = format!("Shader creation error: {}", e);
108        GameError::ShaderProgramError(errstr)
109    }
110}