diff options
author | hozan23 <hozan23@proton.me> | 2024-03-11 09:09:53 +0100 |
---|---|---|
committer | hozan23 <hozan23@proton.me> | 2024-03-11 09:09:53 +0100 |
commit | 6040ab6bd35a47a3c8435c37747b8fcf43c4c7eb (patch) | |
tree | e02d63a575c87f662f8de61341fac7a7fecf81ab /jsonrpc/src/server.rs | |
parent | a16997d75acf57b0be813716d0d4e7cfbc333f58 (diff) |
jsonrpc: Handle client connection failures & perform general cleanup
Diffstat (limited to 'jsonrpc/src/server.rs')
-rw-r--r-- | jsonrpc/src/server.rs | 80 |
1 files changed, 37 insertions, 43 deletions
diff --git a/jsonrpc/src/server.rs b/jsonrpc/src/server.rs index 92920ad..b090d5c 100644 --- a/jsonrpc/src/server.rs +++ b/jsonrpc/src/server.rs @@ -17,6 +17,26 @@ use crate::{ Endpoint, Error, Result, JSONRPC_VERSION, }; +pub const INVALID_REQUEST_ERROR_MSG: &str = "Invalid request"; +pub const FAILED_TO_PARSE_ERROR_MSG: &str = "Failed to parse"; +pub const METHOD_NOT_FOUND_ERROR_MSG: &str = "Method not found"; +pub const INTERNAL_ERROR_MSG: &str = "Internal error"; + +fn pack_err_res(code: i32, msg: &str, id: Option<serde_json::Value>) -> message::Response { + let err = message::Error { + code, + message: msg.to_string(), + data: None, + }; + + message::Response { + jsonrpc: JSONRPC_VERSION.to_string(), + error: Some(err), + result: None, + id, + } +} + /// RPC server config #[derive(Default)] pub struct ServerConfig { @@ -51,7 +71,9 @@ impl<'a> Server<'a> { pub async fn start(self: Arc<Self>) -> Result<()> { loop { let conn = self.listener.accept().await?; - self.handle_conn(conn).await?; + if let Err(err) = self.handle_conn(conn).await { + error!("Failed to handle a new conn: {err}") + } } } @@ -107,7 +129,7 @@ impl<'a> Server<'a> { let rpc_msg = match serde_json::from_slice::<message::Request>(buffer) { Ok(m) => m, Err(_) => { - return self.pack_err_res(message::PARSE_ERROR_CODE, "Failed to parse", None); + return pack_err_res(message::PARSE_ERROR_CODE, FAILED_TO_PARSE_ERROR_MSG, None); } }; @@ -115,9 +137,9 @@ impl<'a> Server<'a> { let srvc_method: Vec<&str> = rpc_msg.method.split('.').collect(); if srvc_method.len() != 2 { - return self.pack_err_res( + return pack_err_res( message::INVALID_REQUEST_ERROR_CODE, - "Invalid request", + INVALID_REQUEST_ERROR_MSG, Some(rpc_msg.id), ); } @@ -130,9 +152,9 @@ impl<'a> Server<'a> { let service = match services.get(srvc_name) { Some(s) => s, None => { - return self.pack_err_res( + return pack_err_res( message::METHOD_NOT_FOUND_ERROR_CODE, - "Method not found", + METHOD_NOT_FOUND_ERROR_MSG, Some(rpc_msg.id), ); } @@ -141,9 +163,9 @@ impl<'a> Server<'a> { let method = match service.get_method(method_name) { Some(m) => m, None => { - return self.pack_err_res( + return pack_err_res( message::METHOD_NOT_FOUND_ERROR_CODE, - "Method not found", + METHOD_NOT_FOUND_ERROR_MSG, Some(rpc_msg.id), ); } @@ -152,33 +174,25 @@ impl<'a> Server<'a> { let result = match method(rpc_msg.params.clone()).await { Ok(res) => res, Err(Error::ParseJSON(_)) => { - return self.pack_err_res( + return pack_err_res( message::PARSE_ERROR_CODE, - "Failed to parse", + FAILED_TO_PARSE_ERROR_MSG, Some(rpc_msg.id), ); } Err(Error::InvalidParams(msg)) => { - return self.pack_err_res( - message::INVALID_PARAMS_ERROR_CODE, - msg, - Some(rpc_msg.id), - ); + return pack_err_res(message::INVALID_PARAMS_ERROR_CODE, msg, Some(rpc_msg.id)); } Err(Error::InvalidRequest(msg)) => { - return self.pack_err_res( - message::INVALID_REQUEST_ERROR_CODE, - msg, - Some(rpc_msg.id), - ); + return pack_err_res(message::INVALID_REQUEST_ERROR_CODE, msg, Some(rpc_msg.id)); } Err(Error::RPCMethodError(code, msg)) => { - return self.pack_err_res(code, msg, Some(rpc_msg.id)); + return pack_err_res(code, msg, Some(rpc_msg.id)); } Err(_) => { - return self.pack_err_res( + return pack_err_res( message::INTERNAL_ERROR_CODE, - "Internal error", + INTERNAL_ERROR_MSG, Some(rpc_msg.id), ); } @@ -191,24 +205,4 @@ impl<'a> Server<'a> { id: Some(rpc_msg.id), } } - - fn pack_err_res( - &self, - code: i32, - msg: &str, - id: Option<serde_json::Value>, - ) -> message::Response { - let err = message::Error { - code, - message: msg.to_string(), - data: None, - }; - - message::Response { - jsonrpc: JSONRPC_VERSION.to_string(), - error: Some(err), - result: None, - id, - } - } } |