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