Skip to content

Commit

Permalink
Merge pull request #385 from EdJoPaTo/errors
Browse files Browse the repository at this point in the history
Improve Error texts
  • Loading branch information
Devdutt Shenoi authored Apr 21, 2022
2 parents 45b8baa + 77bca3f commit 149eca7
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 72 deletions.
2 changes: 1 addition & 1 deletion benchmarks/simplerouter/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum Error {
InvalidReason(u8),
#[error("Invalid protocol used")]
InvalidProtocol,
#[error("Invalid protocol level")]
#[error("Invalid protocol level = {0}")]
InvalidProtocolLevel(u8),
#[error("Invalid packet format")]
IncorrectPacketFormat,
Expand Down
4 changes: 2 additions & 2 deletions rumqttc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum ClientError {
Request(#[from] SendError<Request>),
#[error("Failed to send mqtt requests to eventloop")]
TryRequest(#[from] TrySendError<Request>),
#[error("Serialization error")]
Mqtt4(mqttbytes::Error),
#[error("Serialization error: {0}")]
Mqtt4(#[from] mqttbytes::Error),
}

/// `AsyncClient` to communicate with MQTT `Eventloop`
Expand Down
33 changes: 22 additions & 11 deletions rumqttc/src/mqttbytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! The [`bytes`](https://docs.rs/bytes) crate is used internally.
use bytes::{Buf, BufMut, Bytes, BytesMut};
use core::fmt::{self, Display, Formatter};
use core::fmt;
use std::slice::Iter;

mod topic;
Expand All @@ -13,33 +13,50 @@ pub mod v4;
pub use topic::*;

/// Error during serialization and deserialization
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("Expected Connect, received: {0:?}")]
NotConnect(PacketType),
#[error("Unexpected Connect")]
UnexpectedConnect,
#[error("Invalid Connect return code: {0}")]
InvalidConnectReturnCode(u8),
InvalidReason(u8),
#[error("Invalid protocol")]
InvalidProtocol,
#[error("Invalid protocol level: {0}")]
InvalidProtocolLevel(u8),
#[error("Incorrect packet format")]
IncorrectPacketFormat,
#[error("Invalid packet type: {0}")]
InvalidPacketType(u8),
#[error("Invalid property type: {0}")]
InvalidPropertyType(u8),
InvalidRetainForwardRule(u8),
#[error("Invalid QoS level: {0}")]
InvalidQoS(u8),
#[error("Invalid subscribe reason code: {0}")]
InvalidSubscribeReasonCode(u8),
#[error("Packet id Zero")]
PacketIdZero,
SubscriptionIdZero,
#[error("Payload size is incorrect")]
PayloadSizeIncorrect,
#[error("payload is too long")]
PayloadTooLong,
#[error("payload size limit exceeded: {0}")]
PayloadSizeLimitExceeded(usize),
#[error("Payload required")]
PayloadRequired,
#[error("Topic is not UTF-8")]
TopicNotUtf8,
#[error("Promised boundary crossed: {0}")]
BoundaryCrossed(usize),
#[error("Malformed packet")]
MalformedPacket,
#[error("Malformed remaining length")]
MalformedRemainingLength,
/// More bytes required to frame packet. Argument
/// implies minimum additional bytes required to
/// proceed further
#[error("At least {0} more bytes required to frame packet")]
InsufficientBytes(usize),
}

Expand Down Expand Up @@ -315,9 +332,3 @@ fn read_u8(stream: &mut Bytes) -> Result<u8, Error> {

Ok(stream.get_u8())
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Error = {:?}", self)
}
}
16 changes: 5 additions & 11 deletions rumqttc/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use std::{io, time::Instant};
#[derive(Debug, thiserror::Error)]
pub enum StateError {
/// Io Error while state is passed to network
#[error("Io error {0:?}")]
#[error("Io error: {0:?}")]
Io(#[from] io::Error),
/// Broker's error reply to client's connect packet
#[error("Connect return code `{0:?}`")]
#[error("Connect return code: `{0:?}`")]
Connect(ConnectReturnCode),
/// Invalid state for a given operation
#[error("Invalid state for a given operation")]
InvalidState,
/// Received a packet (ack) which isn't asked for
#[error("Received unsolicited ack pkid {0}")]
#[error("Received unsolicited ack pkid: {0}")]
Unsolicited(u16),
/// Last pingreq isn't acked
#[error("Last pingreq isn't acked")]
Expand All @@ -29,14 +29,8 @@ pub enum StateError {
WrongPacket,
#[error("Timeout while waiting to resolve collision")]
CollisionTimeout,
#[error("Mqtt serialization/deserialization error")]
Deserialization(mqttbytes::Error),
}

impl From<mqttbytes::Error> for StateError {
fn from(e: mqttbytes::Error) -> StateError {
StateError::Deserialization(e)
}
#[error("Mqtt serialization/deserialization error: {0}")]
Deserialization(#[from] mqttbytes::Error),
}

/// State of the mqtt connection.
Expand Down
6 changes: 3 additions & 3 deletions rumqttc/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ use std::sync::Arc;
pub enum Error {
#[error("Addr")]
Addr(#[from] AddrParseError),
#[error("I/O")]
#[error("I/O: {0}")]
Io(#[from] io::Error),
#[error("Web Pki")]
#[error("Web Pki: {0}")]
WebPki(#[from] webpki::Error),
#[error("DNS name")]
DNSName(#[from] InvalidDnsNameError),
#[error("TLS error")]
#[error("TLS error: {0}")]
TLS(#[from] rustls::Error),
#[error("No valid cert in chain")]
NoValidCertInChain,
Expand Down
4 changes: 2 additions & 2 deletions rumqttd/src/async_locallink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use log::{error, trace, warn};

#[derive(Debug, thiserror::Error)]
pub enum LinkError {
#[error("Unexpected router message")]
#[error("Unexpected router message: {0:?}")]
NotConnectionAck(Notification),
#[error("Connack error {0}")]
#[error("Connack error: {0}")]
ConnectionAck(String),
#[error("Channel send error")]
Send(#[from] SendError<(Id, Event)>),
Expand Down
19 changes: 10 additions & 9 deletions rumqttd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ use std::io::BufReader;
#[derive(Debug, thiserror::Error)]
#[error("Acceptor error")]
pub enum Error {
#[error("I/O {0}")]
#[error("I/O: {0}")]
Io(#[from] io::Error),
#[error("Connection error {0}")]
#[error("Connection error: {0}")]
Connection(#[from] remotelink::Error),
#[error("Timeout")]
Timeout(#[from] Elapsed),
Expand All @@ -66,10 +66,10 @@ pub enum Error {
#[error("Channel send error")]
Send(#[from] SendError<(Id, Event)>),
#[cfg(feature = "use-native-tls")]
#[error("Native TLS error {0}")]
#[error("Native TLS error: {0}")]
NativeTls(#[from] NativeTlsError),
#[cfg(feature = "use-rustls")]
#[error("Rustls error {0}")]
#[error("Rustls error: {0}")]
Rustls(#[from] RustlsError),
#[error("Server cert not provided")]
ServerCertRequired,
Expand All @@ -81,18 +81,19 @@ pub enum Error {
ServerCertNotFound(String),
#[error("Server private key file {0} not found")]
ServerKeyNotFound(String),
#[error("Invalid CA cert file {0}")]
#[error("Invalid CA cert file: {0}")]
InvalidCACert(String),
#[error("Invalid server cert file {0}")]
#[error("Invalid server cert file: {0}")]
InvalidServerCert(String),
#[error("Invalid server pass")]
InvalidServerPass(),
#[error("Invalid server key file {0}")]
InvalidServerPass,
#[error("Invalid server key file: {0}")]
InvalidServerKey(String),
RustlsNotEnabled,
NativeTlsNotEnabled,
Disconnected,
NetworkClosed,
#[error("Wrong packet: {0:?}")]
WrongPacket(Packet),
}

Expand Down Expand Up @@ -285,7 +286,7 @@ impl Server {

// Get the identity
let identity = native_tls::Identity::from_pkcs12(&buf, pkcs12_pass)
.map_err(|_| Error::InvalidServerPass())?;
.map_err(|_| Error::InvalidServerPass)?;

// Builder
let builder = native_tls::TlsAcceptor::builder(identity).build()?;
Expand Down
4 changes: 2 additions & 2 deletions rumqttd/src/locallink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use std::time::Instant;

#[derive(Debug, thiserror::Error)]
pub enum LinkError {
#[error("Unexpected router message")]
#[error("Unexpected router message: {0:?}")]
NotConnectionAck(Notification),
#[error("Connack error {0}")]
#[error("Connack error: {0}")]
ConnectionAck(String),
#[error("Channel send error")]
Send(#[from] SendError<(Id, Event)>),
Expand Down
33 changes: 22 additions & 11 deletions rumqttd/src/mqttbytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! The [`bytes`](https://docs.rs/bytes) crate is used internally.
use bytes::{Buf, BufMut, Bytes, BytesMut};
use core::fmt::{self, Display, Formatter};
use core::fmt;
use std::slice::Iter;

mod topic;
Expand All @@ -13,33 +13,50 @@ pub mod v4;
pub use topic::*;

/// Error during serialization and deserialization
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("Expected Connect, received: {0:?}")]
NotConnect(PacketType),
#[error("Unexpected Connect")]
UnexpectedConnect,
#[error("Invalid Connect return code: {0}")]
InvalidConnectReturnCode(u8),
InvalidReason(u8),
#[error("Invalid protocol")]
InvalidProtocol,
#[error("Invalid protocol level: {0}")]
InvalidProtocolLevel(u8),
#[error("Incorrect packet format")]
IncorrectPacketFormat,
#[error("Invalid packet type: {0}")]
InvalidPacketType(u8),
#[error("Invalid property type: {0}")]
InvalidPropertyType(u8),
InvalidRetainForwardRule(u8),
#[error("Invalid QoS level: {0}")]
InvalidQoS(u8),
#[error("Invalid subscribe reason code: {0}")]
InvalidSubscribeReasonCode(u8),
#[error("Packet id Zero")]
PacketIdZero,
SubscriptionIdZero,
#[error("Payload size is incorrect")]
PayloadSizeIncorrect,
#[error("payload is too long")]
PayloadTooLong,
#[error("payload size limit exceeded: {0}")]
PayloadSizeLimitExceeded(usize),
#[error("Payload required")]
PayloadRequired,
#[error("Topic is not UTF-8")]
TopicNotUtf8,
#[error("Promised boundary crossed: {0}")]
BoundaryCrossed(usize),
#[error("Malformed packet")]
MalformedPacket,
#[error("Malformed remaining length")]
MalformedRemainingLength,
/// More bytes required to frame packet. Argument
/// implies minimum additional bytes required to
/// proceed further
#[error("At least {0} more bytes required to frame packet")]
InsufficientBytes(usize),
}

Expand Down Expand Up @@ -316,9 +333,3 @@ fn read_u8(stream: &mut Bytes) -> Result<u8, Error> {

Ok(stream.get_u8())
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Error = {:?}", self)
}
}
10 changes: 5 additions & 5 deletions rumqttd/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use tokio::time::{self, error::Elapsed, Duration};

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O = {0}")]
#[error("I/O: {0}")]
Io(#[from] io::Error),
#[error("State = {0}")]
#[error("State: {0}")]
State(#[from] state::Error),
#[error("Invalid data = {0}")]
Mqtt(mqttbytes::Error),
#[error["Keep alive timeout"]]
#[error("Invalid data: {0}")]
Mqtt(#[from] mqttbytes::Error),
#[error("Keep alive timeout")]
KeepAlive(#[from] Elapsed),
}

Expand Down
12 changes: 6 additions & 6 deletions rumqttd/src/remotelink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ pub struct RemoteLink {

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O")]
#[error("I/O: {0}")]
Io(#[from] io::Error),
#[error("Network {0}")]
#[error("Network: {0}")]
Network(#[from] network::Error),
#[error("Timeout")]
Timeout(#[from] Elapsed),
#[error("State error")]
#[error("State error: {0}")]
State(#[from] state::Error),
#[error("Unexpected router message")]
#[error("Unexpected router message: {0:?}")]
RouterMessage(Notification),
#[error("Connack error {0}")]
#[error("Connack error: {0}")]
ConnAck(String),
#[error("Keep alive time exceeded")]
KeepAlive,
#[error("Channel send error")]
Send(#[from] SendError<(Id, Event)>),
#[error("Channel recv error")]
Recv(#[from] RecvError),
#[error("Payload count greater than max inflight")]
#[error("Payload count ({0}) greater than max inflight")]
TooManyPayloads(usize),
#[error("Persistent session requires valid client id")]
InvalidClientId,
Expand Down
12 changes: 3 additions & 9 deletions rumqttd/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use std::vec::IntoIter;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Received unsolicited ack from the device. {0}")]
#[error("Received unsolicited ack from the device: {0}")]
Unsolicited(u16),
#[error("Collision with an unacked packet")]
Serialization(mqttbytes::Error),
#[error("Collision with an unacked packet: {0}")]
Serialization(#[from] mqttbytes::Error),
#[error("Collision with an unacked packet")]
Collision,
#[error("Duplicate connect")]
Expand All @@ -23,12 +23,6 @@ pub enum Error {
Disconnect,
}

impl From<mqttbytes::Error> for Error {
fn from(e: mqttbytes::Error) -> Error {
Error::Serialization(e)
}
}

#[derive(Debug)]
struct Pending {
topic: String,
Expand Down

0 comments on commit 149eca7

Please sign in to comment.