Skip to main content

manabrew_server/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum ServerError {
5    AuthFailed(String),
6    AuthTimeout,
7    RoomNotFound(String),
8    RoomFull(String),
9    IncorrectPassword,
10    InvalidSeatAssignment(String),
11    NotInRoom,
12    NotHost,
13    PlayersNotReady,
14    DeckNotSelected,
15    AlreadyInRoom(String),
16    GameAlreadyStarted,
17    GameNotInProgress,
18    FormatNotChosen,
19    InvalidDraftConfig(String),
20    InvalidResumeToken,
21    DuplicateUsername(String),
22    PlayerNotFound(String),
23    PlayerInGame(String),
24    InvalidChatMessage(String),
25    ChatRateLimited,
26    AccountRequired,
27    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
28    Serde(serde_json::Error),
29}
30
31impl fmt::Display for ServerError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::AuthFailed(msg) => write!(f, "Authentication failed: {msg}"),
35            Self::AuthTimeout => write!(f, "Authentication timed out"),
36            Self::RoomNotFound(id) => write!(f, "Room not found: {id}"),
37            Self::RoomFull(id) => write!(f, "Room is full: {id}"),
38            Self::IncorrectPassword => write!(f, "Incorrect room password"),
39            Self::InvalidSeatAssignment(msg) => write!(f, "Invalid seat assignment: {msg}"),
40            Self::NotInRoom => write!(f, "You are not in a room"),
41            Self::NotHost => write!(f, "Only the host can do that"),
42            Self::PlayersNotReady => write!(f, "Not all players are ready"),
43            Self::DeckNotSelected => write!(f, "You must select a deck before getting ready"),
44            Self::GameAlreadyStarted => write!(f, "Game has already started"),
45            Self::GameNotInProgress => write!(f, "No game is in progress"),
46            Self::FormatNotChosen => write!(f, "A format must be chosen before starting"),
47            Self::InvalidDraftConfig(msg) => write!(f, "Invalid draft config: {msg}"),
48            Self::InvalidResumeToken => write!(f, "Invalid resume token"),
49            Self::AlreadyInRoom(id) => write!(f, "Already in room: {id}"),
50            Self::DuplicateUsername(name) => write!(f, "Username already taken: {name}"),
51            Self::PlayerNotFound(name) => write!(f, "Player not found: {name}"),
52            Self::PlayerInGame(name) => write!(f, "Player is in a game: {name}"),
53            Self::InvalidChatMessage(msg) => write!(f, "Invalid chat message: {msg}"),
54            Self::ChatRateLimited => write!(f, "You are sending messages too quickly"),
55            Self::AccountRequired => write!(f, "Sign in to chat in General"),
56            Self::WebSocket(e) => write!(f, "WebSocket error: {e}"),
57            Self::Serde(e) => write!(f, "Serialization error: {e}"),
58        }
59    }
60}
61
62impl std::error::Error for ServerError {}
63
64impl ServerError {
65    pub fn code(&self) -> &'static str {
66        match self {
67            Self::AuthFailed(_) => "auth_failed",
68            Self::AuthTimeout => "auth_timeout",
69            Self::RoomNotFound(_) => "room_not_found",
70            Self::RoomFull(_) => "room_full",
71            Self::IncorrectPassword => "incorrect_password",
72            Self::InvalidSeatAssignment(_) => "invalid_seat",
73            Self::NotInRoom => "not_in_room",
74            Self::NotHost => "not_host",
75            Self::PlayersNotReady => "players_not_ready",
76            Self::DeckNotSelected => "deck_not_selected",
77            Self::GameAlreadyStarted => "game_already_started",
78            Self::GameNotInProgress => "game_not_in_progress",
79            Self::FormatNotChosen => "format_not_chosen",
80            Self::InvalidDraftConfig(_) => "invalid_draft_config",
81            Self::InvalidResumeToken => "invalid_resume_token",
82            Self::AlreadyInRoom(_) => "already_in_room",
83            Self::DuplicateUsername(_) => "duplicate_username",
84            Self::PlayerNotFound(_) => "player_not_found",
85            Self::PlayerInGame(_) => "player_in_game",
86            Self::InvalidChatMessage(_) => "invalid_chat_message",
87            Self::ChatRateLimited => "chat_rate_limited",
88            Self::AccountRequired => "account_required",
89            Self::WebSocket(_) => "websocket_error",
90            Self::Serde(_) => "parse_error",
91        }
92    }
93}
94
95impl From<tokio_tungstenite::tungstenite::Error> for ServerError {
96    fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
97        Self::WebSocket(Box::new(e))
98    }
99}
100
101impl From<serde_json::Error> for ServerError {
102    fn from(e: serde_json::Error) -> Self {
103        Self::Serde(e)
104    }
105}