Skip to main content

steamkit/
error.rs

1//! The crate-wide error type.
2
3use std::fmt;
4
5/// Shorthand for `Result<T, SteamError>`.
6pub type SteamResult<T> = Result<T, SteamError>;
7
8/// The error type used throughout the crate.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum SteamError {
12    /// A socket, file or pipe level failure.
13    #[error("i/o error: {0}")]
14    Io(#[from] std::io::Error),
15
16    /// The connection to the Steam network could not be established, or was lost.
17    #[error("connection error: {0}")]
18    Connection(String),
19
20    /// The remote end closed the connection.
21    #[error("connection closed by peer")]
22    ConnectionClosed,
23
24    /// The operation did not complete within its deadline.
25    #[error("timed out after {0:?}")]
26    Timeout(std::time::Duration),
27
28    /// The server rejected the job (`EMsg::DestJobFailed`).
29    #[error("job failed: the request was rejected by the server")]
30    JobFailed,
31
32    /// The operation was cancelled by the caller.
33    #[error("operation was cancelled")]
34    Cancelled,
35
36    /// Authentication failed.
37    #[error("authentication failed: {0}")]
38    Authentication(String),
39
40    /// The peer sent data that does not match the protocol.
41    #[error("protocol error: {0}")]
42    Protocol(String),
43
44    /// A message could not be decoded.
45    #[error("malformed message: {0}")]
46    Malformed(String),
47
48    /// A compressed payload could not be decompressed.
49    #[error("decompression failed: {0}")]
50    Decompression(String),
51
52    /// A cryptographic operation failed.
53    #[error("cryptographic error: {0}")]
54    Crypto(String),
55
56    /// A caller supplied an argument the callee cannot work with.
57    #[error("invalid argument: {0}")]
58    InvalidArgument(String),
59
60    /// The requested resource does not exist.
61    #[error("not found: {0}")]
62    NotFound(String),
63
64    /// JSON (de)serialisation failed.
65    #[error("json error: {0}")]
66    Json(#[from] serde_json::Error),
67
68    /// A protobuf payload could not be decoded.
69    #[error("protobuf decode error: {0}")]
70    Protobuf(#[from] prost::DecodeError),
71
72    /// A protobuf payload could not be encoded.
73    #[error("protobuf encode error: {0}")]
74    ProtobufEncode(#[from] prost::EncodeError),
75
76    /// Anything that does not fit a more specific variant.
77    #[error("{0}")]
78    Other(String),
79}
80
81impl SteamError {
82    /// Builds a [`SteamError::Connection`] from anything displayable.
83    pub fn connection(message: impl fmt::Display) -> Self {
84        SteamError::Connection(message.to_string())
85    }
86
87    /// Builds a [`SteamError::Protocol`] from anything displayable.
88    pub fn protocol(message: impl fmt::Display) -> Self {
89        SteamError::Protocol(message.to_string())
90    }
91
92    /// Builds a [`SteamError::InvalidArgument`] from anything displayable.
93    pub fn invalid_argument(message: impl fmt::Display) -> Self {
94        SteamError::InvalidArgument(message.to_string())
95    }
96}