Skip to main content

jsonrpsee_core/
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 serde::Serialize;
28use serde_json::value::RawValue;
29
30#[derive(Debug, Clone)]
31pub(crate) enum InnerSubscriptionErr {
32    String(String),
33    Json(Box<RawValue>),
34}
35
36/// Error returned when a subscription fails where the error is returned
37/// as special error notification with the following format:
38///
39/// ```json
40/// {"jsonrpc":"2.0", "method":"subscription_error", "params": {"subscription": "sub_id", "error": <error message from this type>}}
41/// ```
42///
43/// It's recommended to use [`SubscriptionError::from_json`] to create a new instance of this error
44/// if the underlying error is a JSON value. That will ensure that the error is serialized
45/// correctly.
46///
47/// SubscriptionError::from will serialize the error as a string, which is not
48/// recommended and should only by used in the value of a `String` type.
49/// It's mainly provided for convenience and to allow for easy conversion any type that implements
50/// StdError.
51#[derive(Debug, Clone)]
52pub struct SubscriptionError(pub(crate) InnerSubscriptionErr);
53
54impl Serialize for SubscriptionError {
55    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
56    where
57        S: serde::Serializer,
58    {
59        match &self.0 {
60            InnerSubscriptionErr::String(s) => serializer.serialize_str(s),
61            InnerSubscriptionErr::Json(json) => json.serialize(serializer),
62        }
63    }
64}
65
66impl<T: ToString> From<T> for SubscriptionError {
67    fn from(val: T) -> Self {
68        Self(InnerSubscriptionErr::String(val.to_string()))
69    }
70}
71
72impl SubscriptionError {
73    /// Create a new `SubscriptionError` from a JSON value.
74    pub fn from_json(json: Box<RawValue>) -> Self {
75        Self(InnerSubscriptionErr::Json(json))
76    }
77}
78
79/// The error returned when registering a method or subscription failed.
80#[derive(Debug, Clone, thiserror::Error)]
81pub enum RegisterMethodError {
82    /// Method was already registered.
83    #[error("Method: {0} was already registered")]
84    AlreadyRegistered(String),
85    /// Subscribe and unsubscribe method names are the same.
86    #[error("Cannot use the same method name for subscribe and unsubscribe, used: {0}")]
87    SubscriptionNameConflict(String),
88    /// Method with that name has not yet been registered.
89    #[error("Method: {0} has not yet been registered")]
90    MethodNotFound(String),
91}