jsonrpsee_core/server/
error.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27use crate::server::SubscriptionMessage;
28use tokio::sync::mpsc;
29
30/// Error that may occur during [`crate::server::MethodSink::try_send`] or [`crate::server::SubscriptionSink::try_send`].
31#[derive(Debug, thiserror::Error)]
32pub enum TrySendError {
33	/// The connection channel is closed.
34	#[error("The connection channel is closed")]
35	Closed(SubscriptionMessage),
36	/// The connection channel is full.
37	#[error("The connection channel is full")]
38	Full(SubscriptionMessage),
39}
40
41/// Error that may occur during [`crate::server::MethodSink::send`] or [`crate::server::SubscriptionSink::send`].
42#[derive(Debug, thiserror::Error)]
43#[error("The connection channel is closed")]
44pub struct DisconnectError(pub SubscriptionMessage);
45
46/// Error that may occur during [`crate::server::MethodSink::send_timeout`] or [`crate::server::SubscriptionSink::send_timeout`].
47#[derive(Debug, thiserror::Error)]
48pub enum SendTimeoutError {
49	/// The data could not be sent because the timeout elapsed
50	/// which most likely is that the channel is full.
51	#[error("The connection channel timed out waiting on send operation")]
52	Timeout(SubscriptionMessage),
53	/// The connection channel is closed.
54	#[error("The connection channel is closed")]
55	Closed(SubscriptionMessage),
56}
57
58/// The error returned while accepting a subscription.
59#[derive(Debug, Copy, Clone, thiserror::Error)]
60#[error("The remote peer closed the connection")]
61pub struct PendingSubscriptionAcceptError;
62
63impl From<mpsc::error::SendError<String>> for DisconnectError {
64	fn from(e: mpsc::error::SendError<String>) -> Self {
65		DisconnectError(SubscriptionMessage::from_complete_message(e.0))
66	}
67}
68
69impl From<mpsc::error::TrySendError<String>> for TrySendError {
70	fn from(e: mpsc::error::TrySendError<String>) -> Self {
71		match e {
72			mpsc::error::TrySendError::Closed(m) => Self::Closed(SubscriptionMessage::from_complete_message(m)),
73			mpsc::error::TrySendError::Full(m) => Self::Full(SubscriptionMessage::from_complete_message(m)),
74		}
75	}
76}
77
78impl From<mpsc::error::SendTimeoutError<String>> for SendTimeoutError {
79	fn from(e: mpsc::error::SendTimeoutError<String>) -> Self {
80		match e {
81			mpsc::error::SendTimeoutError::Closed(m) => Self::Closed(SubscriptionMessage::from_complete_message(m)),
82			mpsc::error::SendTimeoutError::Timeout(m) => Self::Timeout(SubscriptionMessage::from_complete_message(m)),
83		}
84	}
85}