1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! The error type of why errors occur in the cluster module.

use crate::shard::Error as ShardError;
use std::{
    error::Error as StdError,
    fmt::{Display, Formatter, Result as FmtResult},
    result::Result as StdResult,
};
use twilight_http::Error as HttpError;

/// A result enum with the error type being the cluster's [`Error`] type.
///
/// [`Error`]: enum.Error.html
pub type Result<T, E = Error> = StdResult<T, E>;

/// Error type representing the possible reasons for errors to occur in the
/// cluster.
#[allow(clippy::pub_enum_variant_names)]
#[derive(Debug)]
pub enum Error {
    /// An error occurred while getting the gateway information with the number
    /// of shards to use.
    GettingGatewayInfo {
        /// The reason for the error.
        source: HttpError,
    },
    /// The start of the shard range was greater than the end or total.
    IdTooLarge {
        /// The last shard in the range to manage.
        end: u64,
        /// The first shard in the range to manage.
        start: u64,
        /// The total number of shards used by the bot.
        total: u64,
    },
    /// The "large threshold" value was too large or too small.
    LargeThresholdInvalid {
        /// The error from the shard's [`ShardConfigBuilder`].
        ///
        /// [`ShardConfigBuilder`]: ../../shard/config/struct.ShardConfigBuilder.html
        source: ShardError,
    },
    /// The shard you are trying to get does not exist.
    ShardDoesNotExist {
        /// The attempted ID.
        id: u64,
    },
    ShardError {
        /// The error from the shard's [`ShardConfigBuilder`].
        ///
        /// [`ShardConfigBuilder`]: ../../shard/config/struct.ShardConfigBuilder.html
        source: ShardError,
    },
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::GettingGatewayInfo { .. } => f.write_str("Getting the gateway info failed"),
            Self::IdTooLarge { end, start, total } => write!(
                f,
                "The shard ID pair {}-{}/{} is mismatched",
                start, end, total
            ),
            Self::LargeThresholdInvalid { source } | Self::ShardError { source } => {
                write!(f, "{}", source)
            }
            Self::ShardDoesNotExist { id } => write!(f, "The ID: {} does not exist", id),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Self::GettingGatewayInfo { source } => Some(source),
            Self::IdTooLarge { .. } | Self::ShardDoesNotExist { .. } => None,
            Self::LargeThresholdInvalid { source } | Self::ShardError { source } => Some(source),
        }
    }
}