Skip to main content

steamkit/steam/steam_client/
async_job_failed_exception.rs

1//! The error for a job that Steam failed on its end. Converts into
2//! [`SteamError`].
3
4use std::error::Error;
5use std::fmt;
6
7use crate::error::SteamError;
8
9/// Steam hit a remote error while handling a pending job.
10#[derive(Debug)]
11pub struct AsyncJobFailedException {
12    message: String,
13    source: Option<Box<dyn Error + Send + Sync>>,
14}
15
16impl AsyncJobFailedException {
17    /// Creates the error with the default message.
18    pub fn new() -> Self {
19        Self {
20            message: "The job failed remotely.".to_owned(),
21            source: None,
22        }
23    }
24
25    /// Creates the error with the given message.
26    pub fn with_message(message: impl Into<String>) -> Self {
27        Self {
28            message: message.into(),
29            source: None,
30        }
31    }
32
33    /// Creates the error with the given message and cause.
34    pub fn with_cause(message: impl Into<String>, cause: impl Into<Box<dyn Error + Send + Sync>>) -> Self {
35        Self {
36            message: message.into(),
37            source: Some(cause.into()),
38        }
39    }
40
41    /// The message describing the error.
42    pub fn message(&self) -> &str {
43        &self.message
44    }
45}
46
47impl Default for AsyncJobFailedException {
48    fn default() -> Self {
49        Self::new()
50    }
51}
52
53impl fmt::Display for AsyncJobFailedException {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match &self.source {
56            Some(source) => write!(f, "{}: {}", self.message, source),
57            None => f.write_str(&self.message),
58        }
59    }
60}
61
62impl Error for AsyncJobFailedException {
63    fn source(&self) -> Option<&(dyn Error + 'static)> {
64        self.source
65            .as_ref()
66            .map(|source| source.as_ref() as &(dyn Error + 'static))
67    }
68}
69
70impl From<AsyncJobFailedException> for SteamError {
71    fn from(_: AsyncJobFailedException) -> Self {
72        SteamError::JobFailed
73    }
74}