Skip to main content

steamkit/steam/authentication/
authentication_exception.rs

1//! The error type of the authentication flows.
2
3use crate::base::generated::steam_language::EResult;
4use crate::error::SteamError;
5
6/// Returned when `SteamAuthentication` fails to authenticate.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8pub enum AuthenticationException {
9    /// The operation failed with a Steam result code.
10    #[error("{message} with result {result:?}.")]
11    Failed {
12        /// The message that describes the error.
13        message: String,
14        /// The result code that describes the error.
15        result: EResult,
16    },
17
18    /// The operation failed for a reason that has no result code.
19    #[error("{message}")]
20    Other {
21        /// The message that describes the error.
22        message: String,
23    },
24}
25
26impl AuthenticationException {
27    /// Creates an error that carries a Steam result code.
28    pub fn failed(message: impl Into<String>, result: EResult) -> Self {
29        AuthenticationException::Failed {
30            message: message.into(),
31            result,
32        }
33    }
34
35    /// Creates an error without a result code.
36    pub fn other(message: impl Into<String>) -> Self {
37        AuthenticationException::Other {
38            message: message.into(),
39        }
40    }
41
42    /// The result of the authentication request, if the failure carried one.
43    pub fn result(&self) -> Option<EResult> {
44        match self {
45            AuthenticationException::Failed { result, .. } => Some(*result),
46            AuthenticationException::Other { .. } => None,
47        }
48    }
49}
50
51impl From<AuthenticationException> for SteamError {
52    /// Converts into [`SteamError`].
53    fn from(value: AuthenticationException) -> Self {
54        SteamError::Authentication(value.to_string())
55    }
56}