rust-irc/src/error.rs

196 lines
5.2 KiB
Rust
Raw Normal View History

//! Errors for `irc` crate using `failure`.
use std::io::Error as IoError;
use std::sync::mpsc::RecvError;
2019-08-27 15:05:51 +02:00
use futures_channel::{
mpsc::{SendError, TrySendError},
oneshot::Canceled,
};
2019-10-16 03:12:29 +02:00
use thiserror::Error;
2019-08-27 15:05:51 +02:00
use crate::proto::error::{MessageParseError, ProtocolError};
/// A specialized `Result` type for the `irc` crate.
2019-10-16 03:12:29 +02:00
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// The main crate-wide error type.
2019-10-16 03:12:29 +02:00
#[derive(Debug, Error)]
2019-08-27 15:05:51 +02:00
pub enum Error {
/// An internal I/O error.
2019-10-16 03:12:29 +02:00
#[error("an io error occurred")]
Io(#[source] IoError),
/// An internal TLS error.
2019-10-16 03:12:29 +02:00
#[error("a TLS error occurred")]
Tls(#[source] native_tls::Error),
/// An internal synchronous channel closed.
2019-10-16 03:12:29 +02:00
#[error("a sync channel closed")]
SyncChannelClosed(#[source] RecvError),
/// An internal asynchronous channel closed.
2019-10-16 03:12:29 +02:00
#[error("an async channel closed")]
AsyncChannelClosed(#[source] SendError),
/// An internal oneshot channel closed.
2019-10-16 03:12:29 +02:00
#[error("a oneshot channel closed")]
OneShotCanceled(#[source] Canceled),
/// Error for invalid configurations.
2019-10-16 03:12:29 +02:00
#[error("invalid config: {}", path)]
InvalidConfig {
/// The path to the configuration, or "<none>" if none specified.
path: String,
/// The detailed configuration error.
2019-10-16 03:12:29 +02:00
#[source]
cause: ConfigError,
},
/// Error for invalid messages.
2019-10-16 03:12:29 +02:00
#[error("invalid message: {}", string)]
InvalidMessage {
/// The string that failed to parse.
string: String,
/// The detailed message parsing error.
2019-10-16 03:12:29 +02:00
#[source]
cause: MessageParseError,
},
/// Mutex for a logged transport was poisoned making the log inaccessible.
2019-10-16 03:12:29 +02:00
#[error("mutex for a logged transport was poisoned")]
PoisonedLog,
/// Ping timed out due to no response.
2019-10-16 03:12:29 +02:00
#[error("connection reset: no ping response")]
PingTimeout,
/// Failed to lookup an unknown codec.
2019-10-16 03:12:29 +02:00
#[error("unknown codec: {}", codec)]
UnknownCodec {
/// The attempted codec.
codec: String,
},
/// Failed to encode or decode something with the given codec.
2019-10-16 03:12:29 +02:00
#[error("codec {} failed: {}", codec, data)]
CodecFailed {
/// The canonical codec name.
codec: &'static str,
/// The data that failed to encode or decode.
data: String,
},
/// All specified nicknames were in use or unusable.
2019-10-16 03:12:29 +02:00
#[error("none of the specified nicknames were usable")]
NoUsableNick,
2019-08-27 15:05:51 +02:00
/// Stream has already been configured.
2019-10-16 03:12:29 +02:00
#[error("stream has already been configured")]
2019-08-27 15:05:51 +02:00
StreamAlreadyConfigured,
}
/// Errors that occur with configurations.
2019-10-16 03:12:29 +02:00
#[derive(Debug, Error)]
pub enum ConfigError {
/// Failed to parse as TOML.
#[cfg(feature = "toml")]
2019-10-16 03:12:29 +02:00
#[error("invalid toml")]
InvalidToml(#[source] TomlError),
/// Failed to parse as JSON.
#[cfg(feature = "json")]
2019-10-16 03:12:29 +02:00
#[error("invalid json")]
InvalidJson(#[source] serde_json::Error),
/// Failed to parse as YAML.
#[cfg(feature = "yaml")]
2019-10-16 03:12:29 +02:00
#[error("invalid yaml")]
InvalidYaml(#[source] serde_yaml::Error),
/// Failed to parse the given format because it was disabled at compile-time.
2019-10-16 03:12:29 +02:00
#[error("config format disabled: {}", format)]
ConfigFormatDisabled {
/// The disabled file format.
format: &'static str,
},
/// Could not identify the given file format.
2019-10-16 03:12:29 +02:00
#[error("config format unknown: {}", format)]
UnknownConfigFormat {
/// The unknown file extension.
format: String,
},
/// File was missing an extension to identify file format.
2019-10-16 03:12:29 +02:00
#[error("missing format extension")]
MissingExtension,
/// Configuration does not specify a nickname.
2019-10-16 03:12:29 +02:00
#[error("nickname not specified")]
NicknameNotSpecified,
/// Configuration does not specify a server.
2019-10-16 03:12:29 +02:00
#[error("server not specified")]
ServerNotSpecified,
}
/// A wrapper that combines toml's serialization and deserialization errors.
#[cfg(feature = "toml")]
2019-10-16 03:12:29 +02:00
#[derive(Debug, Error)]
pub enum TomlError {
/// A TOML deserialization error.
2019-10-16 03:12:29 +02:00
#[error("deserialization failed")]
Read(#[source] toml::de::Error),
/// A TOML serialization error.
2019-10-16 03:12:29 +02:00
#[error("serialization failed")]
Write(#[source] toml::ser::Error),
}
2019-08-27 15:05:51 +02:00
impl From<ProtocolError> for Error {
fn from(e: ProtocolError) -> Error {
match e {
2019-08-27 15:05:51 +02:00
ProtocolError::Io(e) => Error::Io(e),
ProtocolError::InvalidMessage { string, cause } => {
Error::InvalidMessage { string, cause }
}
}
}
}
2019-08-27 15:05:51 +02:00
impl From<IoError> for Error {
fn from(e: IoError) -> Error {
Error::Io(e)
}
}
2019-10-16 03:12:29 +02:00
impl From<native_tls::Error> for Error {
fn from(e: native_tls::Error) -> Error {
2019-08-27 15:05:51 +02:00
Error::Tls(e)
2019-06-10 20:52:36 -05:00
}
}
2019-08-27 15:05:51 +02:00
impl From<RecvError> for Error {
fn from(e: RecvError) -> Error {
Error::SyncChannelClosed(e)
}
}
2019-08-27 15:05:51 +02:00
impl From<SendError> for Error {
fn from(e: SendError) -> Error {
Error::AsyncChannelClosed(e)
}
}
2019-08-27 15:05:51 +02:00
impl<T> From<TrySendError<T>> for Error {
fn from(e: TrySendError<T>) -> Error {
Error::AsyncChannelClosed(e.into_send_error())
}
}
2017-06-20 14:54:06 -04:00
2019-08-27 15:05:51 +02:00
impl From<Canceled> for Error {
fn from(e: Canceled) -> Error {
Error::OneShotCanceled(e)
2017-06-20 14:54:06 -04:00
}
}