@@ -36,7 +36,8 @@ use tokio::sync::{mpsc, oneshot};
3636
3737use jsonrpsee_types:: response:: SubscriptionError ;
3838use jsonrpsee_types:: {
39- ErrorObject , Id , Notification , RequestSer , Response , ResponseSuccess , SubscriptionId , SubscriptionResponse ,
39+ ErrorObject , Id , InvalidRequestId , Notification , RequestSer , Response , ResponseSuccess , SubscriptionId ,
40+ SubscriptionResponse ,
4041} ;
4142use serde_json:: Value as JsonValue ;
4243use std:: ops:: Range ;
@@ -63,7 +64,7 @@ pub(crate) fn process_batch_response(
6364 Some ( state) => state,
6465 None => {
6566 tracing:: warn!( "Received unknown batch response" ) ;
66- return Err ( Error :: InvalidRequestId ) ;
67+ return Err ( InvalidRequestId :: NotPendingRequest ( format ! ( "{:?}" , range ) ) . into ( ) ) ;
6768 }
6869 } ;
6970
@@ -79,7 +80,7 @@ pub(crate) fn process_batch_response(
7980 if let Some ( elem) = maybe_elem {
8081 * elem = rp. result ;
8182 } else {
82- return Err ( Error :: InvalidRequestId ) ;
83+ return Err ( InvalidRequestId :: NotPendingRequest ( rp . id . to_string ( ) ) . into ( ) ) ;
8384 }
8485 }
8586
@@ -95,7 +96,7 @@ pub(crate) fn process_batch_response(
9596pub ( crate ) fn process_subscription_response (
9697 manager : & mut RequestManager ,
9798 response : SubscriptionResponse < JsonValue > ,
98- ) -> Result < ( ) , Option < RequestMessage > > {
99+ ) -> Result < ( ) , Option < SubscriptionId < ' static > > > {
99100 let sub_id = response. params . subscription . into_owned ( ) ;
100101 let request_id = match manager. get_request_id_by_subscription_id ( & sub_id) {
101102 Some ( request_id) => request_id,
@@ -110,9 +111,7 @@ pub(crate) fn process_subscription_response(
110111 Ok ( ( ) ) => Ok ( ( ) ) ,
111112 Err ( err) => {
112113 tracing:: error!( "Dropping subscription {:?} error: {:?}" , sub_id, err) ;
113- let msg = build_unsubscribe_message ( manager, request_id, sub_id)
114- . expect ( "request ID and subscription ID valid checked above; qed" ) ;
115- Err ( Some ( msg) )
114+ Err ( Some ( sub_id) )
116115 }
117116 } ,
118117 None => {
@@ -124,42 +123,42 @@ pub(crate) fn process_subscription_response(
124123
125124/// Attempts to close a subscription when a [`SubscriptionError`] is received.
126125///
127- /// Returns `Ok(())` if the subscription was removed
128- /// Return `Err(e)` if the subscription was not found.
126+ /// If the notification is not found it's just logged as a warning and the connection
127+ /// will continue.
128+ ///
129+ /// It's possible that the user closed down the subscription before the actual close response is received
129130pub ( crate ) fn process_subscription_close_response (
130131 manager : & mut RequestManager ,
131132 response : SubscriptionError < JsonValue > ,
132- ) -> Result < ( ) , Error > {
133+ ) {
133134 let sub_id = response. params . subscription . into_owned ( ) ;
134- let request_id = match manager. get_request_id_by_subscription_id ( & sub_id) {
135- Some ( request_id) => request_id,
135+ match manager. get_request_id_by_subscription_id ( & sub_id) {
136+ Some ( request_id) => {
137+ manager. remove_subscription ( request_id, sub_id) . expect ( "Both request ID and sub ID in RequestManager; qed" ) ;
138+ }
136139 None => {
137- tracing:: error!( "The server tried to close an invalid subscription: {:?}" , sub_id) ;
138- return Err ( Error :: InvalidSubscriptionId ) ;
140+ tracing:: debug!( "The server tried to close an non-pending subscription: {:?}" , sub_id) ;
139141 }
140- } ;
141-
142- manager. remove_subscription ( request_id, sub_id) . expect ( "Both request ID and sub ID in RequestManager; qed" ) ;
143- Ok ( ( ) )
142+ }
144143}
145144
146145/// Attempts to process an incoming notification
147146///
148- /// Returns Ok() if the response was successfully handled
149- /// Returns Err() if there was no handler for the method
150- pub ( crate ) fn process_notification ( manager : & mut RequestManager , notif : Notification < JsonValue > ) -> Result < ( ) , Error > {
147+ /// If the notification is not found it's just logged as a warning and the connection
148+ /// will continue.
149+ ///
150+ /// It's possible that user close down the subscription before this notification is received.
151+ pub ( crate ) fn process_notification ( manager : & mut RequestManager , notif : Notification < JsonValue > ) {
151152 match manager. as_notification_handler_mut ( notif. method . to_string ( ) ) {
152153 Some ( send_back_sink) => match send_back_sink. try_send ( notif. params ) {
153- Ok ( ( ) ) => Ok ( ( ) ) ,
154+ Ok ( ( ) ) => ( ) ,
154155 Err ( err) => {
155- tracing:: error !( "Error sending notification, dropping handler for {:?} error: {:?}" , notif. method, err) ;
156+ tracing:: warn !( "Could not send notification, dropping handler for {:?} error: {:?}" , notif. method, err) ;
156157 let _ = manager. remove_notification_handler ( notif. method . into_owned ( ) ) ;
157- Err ( Error :: Custom ( err. to_string ( ) ) )
158158 }
159159 } ,
160160 None => {
161- tracing:: error!( "Notification: {:?} not a registered method" , notif. method) ;
162- Err ( Error :: UnregisteredNotification ( notif. method . into_owned ( ) ) )
161+ tracing:: debug!( "Notification: {:?} not a registered method" , notif. method) ;
163162 }
164163 }
165164}
@@ -179,18 +178,19 @@ pub(crate) fn process_single_response(
179178
180179 match manager. request_status ( & response_id) {
181180 RequestStatus :: PendingMethodCall => {
182- let send_back_oneshot = match manager. complete_pending_call ( response_id) {
181+ let send_back_oneshot = match manager. complete_pending_call ( response_id. clone ( ) ) {
183182 Some ( Some ( send) ) => send,
184183 Some ( None ) => return Ok ( None ) ,
185- None => return Err ( Error :: InvalidRequestId ) ,
184+ None => return Err ( InvalidRequestId :: NotPendingRequest ( response_id . to_string ( ) ) . into ( ) ) ,
186185 } ;
187186
188187 let _ = send_back_oneshot. send ( result) ;
189188 Ok ( None )
190189 }
191190 RequestStatus :: PendingSubscription => {
192- let ( unsub_id, send_back_oneshot, unsubscribe_method) =
193- manager. complete_pending_subscription ( response_id. clone ( ) ) . ok_or ( Error :: InvalidRequestId ) ?;
191+ let ( unsub_id, send_back_oneshot, unsubscribe_method) = manager
192+ . complete_pending_subscription ( response_id. clone ( ) )
193+ . ok_or ( InvalidRequestId :: NotPendingRequest ( response_id. to_string ( ) ) ) ?;
194194
195195 let sub_id = result. map ( |r| SubscriptionId :: try_from ( r) . ok ( ) ) ;
196196
@@ -220,23 +220,23 @@ pub(crate) fn process_single_response(
220220 Ok ( None )
221221 }
222222 }
223- RequestStatus :: Subscription | RequestStatus :: Invalid => Err ( Error :: InvalidRequestId ) ,
223+
224+ RequestStatus :: Subscription | RequestStatus :: Invalid => {
225+ Err ( InvalidRequestId :: NotPendingRequest ( response_id. to_string ( ) ) . into ( ) )
226+ }
224227 }
225228}
226229
227230/// Sends an unsubscribe to request to server to indicate
228231/// that the client is not interested in the subscription anymore.
229232//
230233// NOTE: we don't count this a concurrent request as it's part of a subscription.
231- pub ( crate ) async fn stop_subscription (
232- sender : & mut impl TransportSenderT ,
233- manager : & mut RequestManager ,
234+ pub ( crate ) async fn stop_subscription < S : TransportSenderT > (
235+ sender : & mut S ,
234236 unsub : RequestMessage ,
235- ) {
236- if let Err ( e) = sender. send ( unsub. raw ) . await {
237- tracing:: error!( "Send unsubscribe request failed: {:?}" , e) ;
238- let _ = manager. complete_pending_call ( unsub. id ) ;
239- }
237+ ) -> Result < ( ) , S :: Error > {
238+ sender. send ( unsub. raw ) . await ?;
239+ Ok ( ( ) )
240240}
241241
242242/// Builds an unsubscription message.
@@ -245,7 +245,7 @@ pub(crate) fn build_unsubscribe_message(
245245 sub_req_id : Id < ' static > ,
246246 sub_id : SubscriptionId < ' static > ,
247247) -> Option < RequestMessage > {
248- let ( unsub_req_id, _, unsub, sub_id) = manager. remove_subscription ( sub_req_id, sub_id) ?;
248+ let ( unsub_req_id, _, unsub, sub_id) = manager. unsubscribe ( sub_req_id, sub_id) ?;
249249
250250 let mut params = ArrayParams :: new ( ) ;
251251 params. insert ( sub_id) . ok ( ) ?;
0 commit comments