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