Skip to main content

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
31/// [`crate::server::SubscriptionSink::try_send`].
32#[derive(Debug, thiserror::Error)]
33pub enum TrySendError {
34    /// The connection channel is closed.
35    #[error("The connection channel is closed")]
36    Closed(SubscriptionMessage),
37    /// The connection channel is full.
38    #[error("The connection channel is full")]
39    Full(SubscriptionMessage),
40}
41
42/// Error that may occur during [`crate::server::MethodSink::send`] or
43/// [`crate::server::SubscriptionSink::send`].
44#[derive(Debug, thiserror::Error)]
45#[error("The connection channel is closed")]
46pub struct DisconnectError(pub SubscriptionMessage);
47
48/// Error that may occur during [`crate::server::MethodSink::send_timeout`] or
49/// [`crate::server::SubscriptionSink::send_timeout`].
50#[derive(Debug, thiserror::Error)]
51pub enum SendTimeoutError {
52    /// The data could not be sent because the timeout elapsed
53    /// which most likely is that the channel is full.
54    #[error("The connection channel timed out waiting on send operation")]
55    Timeout(SubscriptionMessage),
56    /// The connection channel is closed.
57    #[error("The connection channel is closed")]
58    Closed(SubscriptionMessage),
59}
60
61/// The error returned while accepting a subscription.
62///
63/// Accepting fails if the connection is closed or if the subscription response exceeds the max
64/// response size.
65#[derive(Debug, Copy, Clone, thiserror::Error)]
66#[error("The connection is closed or the subscription response was too big")]
67pub struct PendingSubscriptionAcceptError;
68
69impl From<mpsc::error::SendError<String>> for DisconnectError {
70    fn from(e: mpsc::error::SendError<String>) -> Self {
71        DisconnectError(SubscriptionMessage::from_complete_message(e.0))
72    }
73}
74
75impl From<mpsc::error::TrySendError<String>> for TrySendError {
76    fn from(e: mpsc::error::TrySendError<String>) -> Self {
77        match e {
78            mpsc::error::TrySendError::Closed(m) => {
79                Self::Closed(SubscriptionMessage::from_complete_message(m))
80            }
81            mpsc::error::TrySendError::Full(m) => {
82                Self::Full(SubscriptionMessage::from_complete_message(m))
83            }
84        }
85    }
86}
87
88impl From<mpsc::error::SendTimeoutError<String>> for SendTimeoutError {
89    fn from(e: mpsc::error::SendTimeoutError<String>) -> Self {
90        match e {
91            mpsc::error::SendTimeoutError::Closed(m) => {
92                Self::Closed(SubscriptionMessage::from_complete_message(m))
93            }
94            mpsc::error::SendTimeoutError::Timeout(m) => {
95                Self::Timeout(SubscriptionMessage::from_complete_message(m))
96            }
97        }
98    }
99}