use thiserror instead of derive_more for error handling (#10696)

* use thiserror instead of derive_more for error handling

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Update utils/prometheus/src/lib.rs

* Update utils/prometheus/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Qinxuan Chen
2022-01-26 03:48:46 +08:00
committed by GitHub
parent 38d94d6323
commit e956c2e1c7
47 changed files with 378 additions and 357 deletions
+17 -9
View File
@@ -313,21 +313,29 @@ impl<B: BlockT> NetworkBehaviour for Bitswap<B> {
}
/// Bitswap protocol error.
#[derive(derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum BitswapError {
/// Protobuf decoding error.
#[display(fmt = "Failed to decode request: {}.", _0)]
DecodeProto(prost::DecodeError),
#[error("Failed to decode request: {0}.")]
DecodeProto(#[from] prost::DecodeError),
/// Protobuf encoding error.
#[display(fmt = "Failed to encode response: {}.", _0)]
EncodeProto(prost::EncodeError),
#[error("Failed to encode response: {0}.")]
EncodeProto(#[from] prost::EncodeError),
/// Client backend error.
Client(sp_blockchain::Error),
#[error(transparent)]
Client(#[from] sp_blockchain::Error),
/// Error parsing CID
BadCid(cid::Error),
#[error(transparent)]
BadCid(#[from] cid::Error),
/// Packet read error.
Read(io::Error),
#[error(transparent)]
Read(#[from] io::Error),
/// Error sending response.
#[display(fmt = "Failed to send response.")]
#[error("Failed to send response.")]
SendResponse,
}
@@ -379,19 +379,20 @@ impl<B: BlockT> BlockRequestHandler<B> {
}
}
#[derive(derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
enum HandleRequestError {
#[display(fmt = "Failed to decode request: {}.", _0)]
DecodeProto(prost::DecodeError),
#[display(fmt = "Failed to encode response: {}.", _0)]
EncodeProto(prost::EncodeError),
#[display(fmt = "Failed to decode block hash: {}.", _0)]
DecodeScale(codec::Error),
#[display(fmt = "Missing `BlockRequest::from_block` field.")]
#[error("Failed to decode request: {0}.")]
DecodeProto(#[from] prost::DecodeError),
#[error("Failed to encode response: {0}.")]
EncodeProto(#[from] prost::EncodeError),
#[error("Failed to decode block hash: {0}.")]
DecodeScale(#[from] codec::Error),
#[error("Missing `BlockRequest::from_block` field.")]
MissingFromField,
#[display(fmt = "Failed to parse BlockRequest::direction.")]
#[error("Failed to parse BlockRequest::direction.")]
ParseDirection,
Client(sp_blockchain::Error),
#[display(fmt = "Failed to send response.")]
#[error(transparent)]
Client(#[from] sp_blockchain::Error),
#[error("Failed to send response.")]
SendResponse,
}
+13 -26
View File
@@ -27,18 +27,18 @@ use std::{borrow::Cow, fmt};
pub type Result<T> = std::result::Result<T, Error>;
/// Error type for the network.
#[derive(derive_more::Display, derive_more::From)]
#[derive(thiserror::Error)]
pub enum Error {
/// Io error
Io(std::io::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
/// Client error
Client(Box<sp_blockchain::Error>),
#[error(transparent)]
Client(#[from] Box<sp_blockchain::Error>),
/// The same bootnode (based on address) is registered with two different peer ids.
#[display(
fmt = "The same bootnode (`{}`) is registered with two different peer ids: `{}` and `{}`",
address,
first_id,
second_id
#[error(
"The same bootnode (`{address}`) is registered with two different peer ids: `{first_id}` and `{second_id}`"
)]
DuplicateBootnode {
/// The address of the bootnode.
@@ -49,11 +49,11 @@ pub enum Error {
second_id: PeerId,
},
/// Prometheus metrics error.
Prometheus(prometheus_endpoint::PrometheusError),
#[error(transparent)]
Prometheus(#[from] prometheus_endpoint::PrometheusError),
/// The network addresses are invalid because they don't match the transport.
#[display(
fmt = "The following addresses are invalid because they don't match the transport: {:?}",
addresses
#[error(
"The following addresses are invalid because they don't match the transport: {addresses:?}"
)]
AddressesForAnotherTransport {
/// Transport used.
@@ -62,7 +62,7 @@ pub enum Error {
addresses: Vec<Multiaddr>,
},
/// The same request-response protocol has been registered multiple times.
#[display(fmt = "Request-response protocol registered multiple times: {}", protocol)]
#[error("Request-response protocol registered multiple times: {protocol}")]
DuplicateRequestResponseProtocol {
/// Name of the protocol registered multiple times.
protocol: Cow<'static, str>,
@@ -75,16 +75,3 @@ impl fmt::Debug for Error {
fmt::Display::fmt(self, f)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(ref err) => Some(err),
Self::Client(ref err) => Some(err),
Self::Prometheus(ref err) => Some(err),
Self::DuplicateBootnode { .. } |
Self::AddressesForAnotherTransport { .. } |
Self::DuplicateRequestResponseProtocol { .. } => None,
}
}
}
@@ -284,20 +284,20 @@ impl<B: Block> LightClientRequestHandler<B> {
}
}
#[derive(derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
enum HandleRequestError {
#[display(fmt = "Failed to decode request: {}.", _0)]
DecodeProto(prost::DecodeError),
#[display(fmt = "Failed to encode response: {}.", _0)]
EncodeProto(prost::EncodeError),
#[display(fmt = "Failed to send response.")]
#[error("Failed to decode request: {0}.")]
DecodeProto(#[from] prost::DecodeError),
#[error("Failed to encode response: {0}.")]
EncodeProto(#[from] prost::EncodeError),
#[error("Failed to send response.")]
SendResponse,
/// A bad request has been received.
#[display(fmt = "bad request: {}", _0)]
#[error("bad request: {0}")]
BadRequest(&'static str),
/// Encoding or decoding of some data failed.
#[display(fmt = "codec error: {}", _0)]
Codec(codec::Error),
#[error("codec error: {0}")]
Codec(#[from] codec::Error),
}
fn fmt_keys(first: Option<&Vec<u8>>, last: Option<&Vec<u8>>) -> String {
@@ -457,9 +457,9 @@ impl<'a> Ready<'a> {
}
/// Error specific to the collection of protocols.
#[derive(Debug, derive_more::Display, derive_more::Error)]
#[derive(Debug, thiserror::Error)]
pub enum NotifsHandlerError {
/// Channel of synchronous notifications is full.
#[error("Channel of synchronous notifications is full.")]
SyncNotificationsClogged,
}
@@ -457,13 +457,14 @@ where
}
/// Error generated by sending on a notifications out substream.
#[derive(Debug, derive_more::From, derive_more::Display)]
#[derive(Debug, thiserror::Error)]
pub enum NotificationsHandshakeError {
/// I/O error on the substream.
Io(io::Error),
#[error(transparent)]
Io(#[from] io::Error),
/// Initial message or handshake was too large.
#[display(fmt = "Initial message or handshake was too large: {}", requested)]
#[error("Initial message or handshake was too large: {requested}")]
TooLarge {
/// Size requested by the remote.
requested: usize,
@@ -472,7 +473,8 @@ pub enum NotificationsHandshakeError {
},
/// Error while decoding the variable-length integer.
VarintDecode(unsigned_varint::decode::Error),
#[error(transparent)]
VarintDecode(#[from] unsigned_varint::decode::Error),
}
impl From<unsigned_varint::io::ReadError> for NotificationsHandshakeError {
@@ -489,10 +491,11 @@ impl From<unsigned_varint::io::ReadError> for NotificationsHandshakeError {
}
/// Error generated by sending on a notifications out substream.
#[derive(Debug, derive_more::From, derive_more::Display)]
#[derive(Debug, thiserror::Error)]
pub enum NotificationsOutError {
/// I/O error on the substream.
Io(io::Error),
#[error(transparent)]
Io(#[from] io::Error),
}
#[cfg(test)]
@@ -884,34 +884,35 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
}
/// Error when registering a protocol.
#[derive(Debug, derive_more::Display, derive_more::Error)]
#[derive(Debug, thiserror::Error)]
pub enum RegisterError {
/// A protocol has been specified multiple times.
DuplicateProtocol(#[error(ignore)] Cow<'static, str>),
#[error("{0}")]
DuplicateProtocol(Cow<'static, str>),
}
/// Error in a request.
#[derive(Debug, derive_more::Display, derive_more::Error)]
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum RequestFailure {
/// We are not currently connected to the requested peer.
#[error("We are not currently connected to the requested peer.")]
NotConnected,
/// Given protocol hasn't been registered.
#[error("Given protocol hasn't been registered.")]
UnknownProtocol,
/// Remote has closed the substream before answering, thereby signaling that it considers the
/// request as valid, but refused to answer it.
#[error("Remote has closed the substream before answering, thereby signaling that it considers the request as valid, but refused to answer it.")]
Refused,
/// The remote replied, but the local node is no longer interested in the response.
#[error("The remote replied, but the local node is no longer interested in the response.")]
Obsolete,
/// Problem on the network.
#[display(fmt = "Problem on the network: {}", _0)]
#[error("Problem on the network: {0}")]
Network(OutboundFailure),
}
/// Error when processing a request sent by a remote.
#[derive(Debug, derive_more::Display, derive_more::Error)]
#[derive(Debug, thiserror::Error)]
pub enum ResponseFailure {
/// Problem on the network.
#[display(fmt = "Problem on the network: {}", _0)]
#[error("Problem on the network: {0}")]
Network(InboundFailure),
}
+3 -1
View File
@@ -1391,7 +1391,7 @@ impl<'a> NotificationSenderReady<'a> {
}
/// Error returned by [`NetworkService::send_notification`].
#[derive(Debug, derive_more::Display, derive_more::Error)]
#[derive(Debug, thiserror::Error)]
pub enum NotificationSenderError {
/// The notification receiver has been closed, usually because the underlying connection
/// closed.
@@ -1399,8 +1399,10 @@ pub enum NotificationSenderError {
/// Some of the notifications most recently sent may not have been received. However,
/// the peer may still be connected and a new `NotificationSender` for the same
/// protocol obtained from [`NetworkService::notification_sender`].
#[error("The notification receiver has been closed")]
Closed,
/// Protocol name hasn't been registered.
#[error("Protocol name hasn't been registered")]
BadProtocol,
}
@@ -241,15 +241,20 @@ impl<B: BlockT> StateRequestHandler<B> {
}
}
#[derive(derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
enum HandleRequestError {
#[display(fmt = "Failed to decode request: {}.", _0)]
DecodeProto(prost::DecodeError),
#[display(fmt = "Failed to encode response: {}.", _0)]
EncodeProto(prost::EncodeError),
#[display(fmt = "Failed to decode block hash: {}.", _0)]
InvalidHash(codec::Error),
Client(sp_blockchain::Error),
#[display(fmt = "Failed to send response.")]
#[error("Failed to decode request: {0}.")]
DecodeProto(#[from] prost::DecodeError),
#[error("Failed to encode response: {0}.")]
EncodeProto(#[from] prost::EncodeError),
#[error("Failed to decode block hash: {0}.")]
InvalidHash(#[from] codec::Error),
#[error(transparent)]
Client(#[from] sp_blockchain::Error),
#[error("Failed to send response.")]
SendResponse,
}
@@ -149,18 +149,23 @@ impl<TBlock: BlockT> RequestHandler<TBlock> {
}
}
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
enum HandleRequestError {
#[display(fmt = "Failed to decode request: {}.", _0)]
DecodeProto(prost::DecodeError),
#[display(fmt = "Failed to encode response: {}.", _0)]
EncodeProto(prost::EncodeError),
#[display(fmt = "Failed to decode block hash: {}.", _0)]
DecodeScale(codec::Error),
Client(sp_blockchain::Error),
#[from(ignore)]
#[display(fmt = "Invalid request {}.", _0)]
InvalidRequest(Box<dyn std::error::Error + Send + Sync>),
#[display(fmt = "Failed to send response.")]
#[error("Failed to decode request: {0}.")]
DecodeProto(#[from] prost::DecodeError),
#[error("Failed to encode response: {0}.")]
EncodeProto(#[from] prost::EncodeError),
#[error("Failed to decode block hash: {0}.")]
DecodeScale(#[from] codec::Error),
#[error(transparent)]
Client(#[from] sp_blockchain::Error),
#[error("Invalid request {0}.")]
InvalidRequest(#[from] Box<dyn std::error::Error + Send + Sync>),
#[error("Failed to send response.")]
SendResponse,
}