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 serde_json::value::RawValue;
29use tokio::sync::mpsc;
30
31/// Error that may occur during [`crate::server::MethodSink::try_send`] or [`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 [`crate::server::SubscriptionSink::send`].
43#[derive(Debug, thiserror::Error)]
44#[error("The connection channel is closed")]
45pub struct DisconnectError(pub SubscriptionMessage);
46
47/// Error that may occur during [`crate::server::MethodSink::send_timeout`] or [`crate::server::SubscriptionSink::send_timeout`].
48#[derive(Debug, thiserror::Error)]
49pub enum SendTimeoutError {
50	/// The data could not be sent because the timeout elapsed
51	/// which most likely is that the channel is full.
52	#[error("The connection channel timed out waiting on send operation")]
53	Timeout(SubscriptionMessage),
54	/// The connection channel is closed.
55	#[error("The connection channel is closed")]
56	Closed(SubscriptionMessage),
57}
58
59/// The error returned while accepting a subscription.
60#[derive(Debug, Copy, Clone, thiserror::Error)]
61#[error("The remote peer closed the connection")]
62pub struct PendingSubscriptionAcceptError;
63
64impl From<mpsc::error::SendError<Box<RawValue>>> for DisconnectError {
65	fn from(e: mpsc::error::SendError<Box<RawValue>>) -> Self {
66		DisconnectError(SubscriptionMessage::from_complete_message(e.0))
67	}
68}
69
70impl From<mpsc::error::TrySendError<Box<RawValue>>> for TrySendError {
71	fn from(e: mpsc::error::TrySendError<Box<RawValue>>) -> Self {
72		match e {
73			mpsc::error::TrySendError::Closed(m) => Self::Closed(SubscriptionMessage::from_complete_message(m)),
74			mpsc::error::TrySendError::Full(m) => Self::Full(SubscriptionMessage::from_complete_message(m)),
75		}
76	}
77}
78
79impl From<mpsc::error::SendTimeoutError<Box<RawValue>>> for SendTimeoutError {
80	fn from(e: mpsc::error::SendTimeoutError<Box<RawValue>>) -> Self {
81		match e {
82			mpsc::error::SendTimeoutError::Closed(m) => Self::Closed(SubscriptionMessage::from_complete_message(m)),
83			mpsc::error::SendTimeoutError::Timeout(m) => Self::Timeout(SubscriptionMessage::from_complete_message(m)),
84		}
85	}
86}