ggrs/
error.rs

1use std::error::Error;
2use std::fmt;
3use std::fmt::Display;
4
5use crate::Frame;
6
7/// This enum contains all error messages this library can return. Most API functions will generally return a [`Result<(), GgrsError>`].
8///
9/// [`Result<(), GgrsError>`]: std::result::Result
10#[derive(Debug, Clone, PartialEq, Hash)]
11pub enum GgrsError {
12    /// When the prediction threshold has been reached, we cannot accept more inputs from the local player.
13    PredictionThreshold,
14    /// You made an invalid request, usually by using wrong parameters for function calls.
15    InvalidRequest {
16        /// Further specifies why the request was invalid.
17        info: String,
18    },
19    /// In a [`SyncTestSession`], this error is returned if checksums of resimulated frames do not match up with the original checksum.
20    ///
21    /// [`SyncTestSession`]: crate::SyncTestSession
22    MismatchedChecksum {
23        /// The frame at which the mismatch occurred.
24        current_frame: Frame,
25        /// The frames with mismatched checksums (one or more)
26        mismatched_frames: Vec<Frame>,
27    },
28    /// The Session is not synchronized yet. Please start the session and wait a few ms to let the clients synchronize.
29    NotSynchronized,
30    /// The spectator got so far behind the host that catching up is impossible.
31    SpectatorTooFarBehind,
32}
33
34impl Display for GgrsError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            GgrsError::PredictionThreshold => {
38                write!(
39                    f,
40                    "Prediction threshold is reached, cannot proceed without catching up."
41                )
42            }
43            GgrsError::InvalidRequest { info } => {
44                write!(f, "Invalid Request: {}", info)
45            }
46            GgrsError::NotSynchronized => {
47                write!(
48                    f,
49                    "The session is not yet synchronized with all remote sessions."
50                )
51            }
52            GgrsError::MismatchedChecksum {
53                current_frame,
54                mismatched_frames,
55            } => {
56                write!(
57                    f,
58                    "Detected checksum mismatch during rollback on frame {}, mismatched frames: {:?}",
59                    current_frame, mismatched_frames
60                )
61            }
62            GgrsError::SpectatorTooFarBehind => {
63                write!(
64                    f,
65                    "The spectator got so far behind the host that catching up is impossible."
66                )
67            }
68        }
69    }
70}
71
72impl Error for GgrsError {}