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
15pub type Result<T, E = Error> = StdResult<T, E>;
16pub type AnyResult<T> = anyhow::Result<T>;
17
18#[derive(Debug, thiserror::Error)]
19pub enum Error {
20  #[error("Incorrect username or password")]
21  IncorrectUserCredentials,
22
23  #[error("Incorrect world password")]
24  IncorrectWorldCredentials(WorldId),
25
26  #[error("Missing password")]
27  MissingPassword,
28
29  #[error("World limit reached")]
30  WorldLimitReached,
31
32  #[error("World not found")]
33  WorldNotFound(WorldId),
34
35  #[error(transparent)]
36  Core(#[from] CoreError),
37  #[error(transparent)]
38  Database(#[from] DatabaseError),
39  #[error(transparent)]
40  Io(#[from] io::Error),
41  #[error(transparent)]
42  Unknown(#[from] anyhow::Error),
43}
44
45impl Serialize for Error {
46  fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
47  where
48    S: Serializer,
49  {
50    serializer.serialize_str(self.to_string().as_str())
51  }
52}
53
54impl<E> From<Result<Infallible, E>> for Error
55where
56  E: Into<Error>,
57{
58  fn from(value: Result<Infallible, E>) -> Self {
59    value.unwrap_err().into()
60  }
61}
62
63impl From<io::ErrorKind> for Error {
64  fn from(value: io::ErrorKind) -> Self {
65    Self::Io(io::Error::from(value))
66  }
67}
68
69impl From<JoinError> for Error {
70  fn from(err: JoinError) -> Self {
71    Self::Io(io::Error::from(err))
72  }
73}