Skip to main content

steamkit/steam/webapi/
web_api_request_exception.rs

1//! The error type of failed Web API requests.
2
3use crate::error::SteamError;
4
5/// A failed Web API request.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7pub enum WebAPIRequestException {
8    /// The server answered with a non-success status code.
9    #[error("Response status code does not indicate success: {status} ({reason}).")]
10    Status {
11        /// The HTTP status code of the response.
12        status: u16,
13        /// The reason phrase of the response.
14        reason: String,
15    },
16
17    /// The request failed for some other reason.
18    #[error("{message}")]
19    Other {
20        /// The message that describes the error.
21        message: String,
22    },
23}
24
25impl WebAPIRequestException {
26    /// Creates the error for a non-success response.
27    pub fn status(status: u16, reason: impl Into<String>) -> Self {
28        WebAPIRequestException::Status {
29            status,
30            reason: reason.into(),
31        }
32    }
33
34    /// Creates the error for a failure without a status code.
35    pub fn other(message: impl Into<String>) -> Self {
36        WebAPIRequestException::Other {
37            message: message.into(),
38        }
39    }
40}
41
42impl From<WebAPIRequestException> for SteamError {
43    /// Converts into [`SteamError`].
44    fn from(value: WebAPIRequestException) -> Self {
45        let message = value.to_string();
46
47        match value {
48            WebAPIRequestException::Status { .. } => SteamError::Connection(message),
49            WebAPIRequestException::Other { .. } => SteamError::Other(message),
50        }
51    }
52}