diff options
| author | Zeyla Hellyer <[email protected]> | 2017-05-22 17:02:00 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-05-22 17:02:00 -0700 |
| commit | 9969be60cf320797c37b317da24d9a08fd5eafa5 (patch) | |
| tree | f27bf7a57af95bbc11990b1edcea9cca99276964 /src/client/error.rs | |
| parent | Reasonably derive Debug on items (diff) | |
| download | serenity-9969be60cf320797c37b317da24d9a08fd5eafa5.tar.xz serenity-9969be60cf320797c37b317da24d9a08fd5eafa5.zip | |
Restructure modules
Modules are now separated into a fashion where the library can be used
for most use cases, without needing to compile the rest.
The core of serenity, with no features enabled, contains only the
struct (model) definitions, constants, and prelude. Models do not have
most functions compiled in, as that is separated into the `model`
feature.
The `client` module has been split into 3 modules: `client`, `gateway`,
and `http`.
`http` contains functions to interact with the REST API. `gateway`
contains the Shard to interact with the gateway, requiring `http` for
retrieving the gateway URL. `client` requires both of the other features
and acts as an abstracted interface over both the gateway and REST APIs,
handling the event loop.
The `builder` module has been separated from `utils`, and can now be
optionally compiled in. It and the `http` feature are required by the
`model` feature due to a large number of methods requiring access to
them.
`utils` now contains a number of utilities, such as the Colour struct, the
`MessageBuilder`, and mention parsing functions.
Each of the original `ext` modules are still featured, with `cache` not
requiring any feature to be enabled, `framework` requiring the `client`,
`model`, and `utils`, and `voice` requiring `gateway`.
In total the features and their requirements are:
- `builder`: none
- `cache`: none
- `client`: `gateway`, `http`
- `framework`: `client`, `model`, `utils`
- `gateway`: `http`
- `http`: none
- `model`: `builder`, `http`
- `utils`: none
- `voice`: `gateway`
The default features are `builder`, `cache`, `client`, `framework`,
`gateway`, `model`, `http`, and `utils`.
To help with forwards compatibility, modules have been re-exported from
their original locations.
Diffstat (limited to 'src/client/error.rs')
| -rw-r--r-- | src/client/error.rs | 149 |
1 files changed, 18 insertions, 131 deletions
diff --git a/src/client/error.rs b/src/client/error.rs index a3df57c..a08d15b 100644 --- a/src/client/error.rs +++ b/src/client/error.rs @@ -1,152 +1,39 @@ -use hyper::status::StatusCode; -use ::constants::ErrorCode; -use ::model::{ChannelType, Permissions}; +use std::error::Error as StdError; +use std::fmt::{Display, Formatter, Result as FmtResult}; -/// An error returned from the [`Client`] or the [`Context`], or model instance. +/// An error returned from the [`Client`]. /// /// This is always wrapped within the library's generic [`Error::Client`] /// variant. /// -/// # Examples -/// -/// Matching an [`Error`] with this variant would look something like the -/// following for the [`GuildId::ban`] method, which in this example is used to -/// re-ban all members with an odd discriminator: -/// -/// ```rust,no_run -/// use serenity::client::{Client, ClientError}; -/// use serenity::Error; -/// use std::env; -/// -/// let token = env::var("DISCORD_BOT_TOKEN").unwrap(); -/// let mut client = Client::login(&token); -/// -/// client.on_member_unban(|context, guild_id, user| { -/// // If the user has an even discriminator, don't re-ban them. -/// if user.discriminator % 2 == 0 { -/// return; -/// } -/// -/// match guild_id.ban(user, 8) { -/// Ok(()) => { -/// // Ban successful. -/// }, -/// Err(Error::Client(ClientError::DeleteMessageDaysAmount(amount))) => { -/// println!("Failed deleting {} days' worth of messages", amount); -/// }, -/// Err(why) => { -/// println!("Unexpected error: {:?}", why); -/// }, -/// } -/// }); -/// ``` -/// /// [`Client`]: struct.Client.html -/// [`Context`]: struct.Context.html /// [`Error`]: ../enum.Error.html /// [`Error::Client`]: ../enum.Error.html#variant.Client /// [`GuildId::ban`]: ../model/struct.GuildId.html#method.ban #[allow(enum_variant_names)] #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub enum Error { - /// When attempting to delete below or above the minimum and maximum allowed - /// number of messages. - BulkDeleteAmount, - /// When attempting to delete a number of days' worth of messages that is - /// not allowed. - DeleteMessageDaysAmount(u8), - /// Indicates that the textual content of an embed exceeds the maximum - /// length. - EmbedTooLarge(u64), - /// When there is an error from Discord for a specific action, such as - /// [`ErrorCode::EditByOtherAuthor`]. This is a friendlier representation of - /// the numerical error codes Discord provides. - /// - /// [`ErrorCode::EditByOtherAuthor`]: rest/enum.ErrorCode.html#variant.EditByOtherAuthor - ErrorCode(ErrorCode), - /// When there was an error retrieving the gateway URI from the REST API. - Gateway, - /// An indication that a [guild][`Guild`] could not be found by - /// [Id][`GuildId`] in the [`Cache`]. - /// - /// [`Guild`]: ../model/struct.Guild.html - /// [`GuildId`]: ../model/struct.GuildId.html - /// [`Cache`]: ../ext/cache/struct.Cache.html - GuildNotFound, - /// An indicator that an unknown opcode was received from the gateway. - InvalidOpCode, - /// Indicates that you do not have the required permissions to perform an - /// operation. - /// - /// The provided [`Permission`]s is the set of required permissions - /// required. - /// - /// [`Permission`]: ../model/permissions/struct.Permissions.html - InvalidPermissions(Permissions), - /// An indicator that the shard data received from the gateway is invalid. - InvalidShards, /// When the token provided is invalid. This is returned when validating a /// token through the [`validate_token`] function. /// /// [`validate_token`]: fn.validate_token.html InvalidToken, - /// An indicator that the [current user] can not perform an action. - /// - /// [current user]: ../model/struct.CurrentUser.html - InvalidUser, - /// An indicator that an item is missing from the [`Cache`], and the action - /// can not be continued. - /// - /// [`Cache`]: ../ext/cache/struct.Cache.html - ItemMissing, - /// Indicates that a [`Message`]s content was too long and will not - /// successfully send, as the length is over 2000 codepoints, or 4000 bytes. - /// - /// The number of bytes larger than the limit is provided. - /// - /// [`Message`]: ../model/struct.Message.html - MessageTooLong(u64), - /// When attempting to use a [`Context`] helper method which requires a - /// contextual [`ChannelId`], but the current context is not appropriate for - /// the action. - /// - /// [`ChannelId`]: ../model/struct.ChannelId.html - /// [`Context`]: struct.Context.html - NoChannelId, - /// When the decoding of a ratelimit header could not be properly decoded - /// into an `i64`. - RateLimitI64, - /// When the decoding of a ratelimit header could not be properly decoded - /// from UTF-8. - RateLimitUtf8, - /// When attempting to find a required record from the Cache could not be - /// found. This is required in methods such as [`Context::edit_role`]. - /// - /// [`Context::edit_role`]: struct.Context.html#method.edit_role - RecordNotFound, /// When a shard has completely failed to reboot after resume and/or /// reconnect attempts. ShardBootFailure, - /// When the shard being retrieved from within the Client could not be - /// found after being inserted into the Client's internal vector of - /// [`Shard`]s. - /// - /// This can be returned from one of the options for starting one or - /// multiple shards. - /// - /// **This should never be received.** - /// - /// [`Shard`]: gateway/struct.Shard.html - ShardUnknown, - /// When a function such as [`Context::edit_channel`] did not expect the - /// received [`ChannelType`]. - /// - /// [`ChannelType`]: ../model/enum.ChannelType.html - /// [`Context::edit_channel`]: struct.Context.html#method.edit_channel - UnexpectedChannelType(ChannelType), - /// When a status code was unexpectedly received for a request's status. - InvalidRequest(StatusCode), - /// When a status is received, but the verification to ensure the response - /// is valid does not recognize the status. - UnknownStatus(u16), +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + f.write_str(self.description()) + } +} + +impl StdError for Error { + fn description(&self) -> &str { + match *self { + Error::InvalidToken => "The provided token was invalid", + Error::ShardBootFailure => "Failed to (re-)boot a shard", + } + } } |