Skip to main content

nil_server/
error.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use nil_core::world::config::WorldId;
5use serde::Serialize;
6use serde::ser::Serializer;
7use std::convert::Infallible;
8use std::io;
9use std::result::Result as StdResult;
10use tokio::task::JoinError;
11
12pub use nil_core::error::Error as CoreError;
13pub use nil_server_database::error::Error as DatabaseError;
14
15#[doc(hidden)]
16pub type Result<T, E = Error> = StdResult<T, E>;
17#[doc(hidden)]
18pub type AnyResult<T> = anyhow::Result<T>;
19
20#[derive(Debug, thiserror::Error)]
21pub enum Error {
22  #[error("Incorrect username or password")]
23  IncorrectUserCredentials,
24
25  #[error("Incorrect world password")]
26  IncorrectWorldCredentials(WorldId),
27
28  #[error("Expected at most {max} characters, got {current}")]
29  MaxCharactersExceeded { max: usize, current: usize },
30
31  #[error("Missing password")]
32  MissingPassword,
33
34  #[error("World limit reached")]
35  WorldLimitReached,
36
37  #[error("World not found")]
38  WorldNotFound(WorldId),
39
40  #[error(transparent)]
41  Core(#[from] CoreError),
42  #[error(transparent)]
43  Database(#[from] DatabaseError),
44  #[error(transparent)]
45  Io(#[from] io::Error),
46  #[error(transparent)]
47  Unknown(#[from] anyhow::Error),
48}
49
50impl Serialize for Error {
51  fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
52  where
53    S: Serializer,
54  {
55    serializer.serialize_str(self.to_string().as_str())
56  }
57}
58
59impl<E> From<Result<Infallible, E>> for Error
60where
61  E: Into<Error>,
62{
63  fn from(value: Result<Infallible, E>) -> Self {
64    value.unwrap_err().into()
65  }
66}
67
68impl From<io::ErrorKind> for Error {
69  fn from(value: io::ErrorKind) -> Self {
70    Self::Io(io::Error::from(value))
71  }
72}
73
74impl From<JoinError> for Error {
75  fn from(err: JoinError) -> Self {
76    Self::Io(io::Error::from(err))
77  }
78}