From 8898b6f359823ee0b5b7b59bb14947c7a3c9c9bb Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 14 May 2022 15:39:20 +0530 Subject: [PATCH] Removing unwraps --- src/executors/mod.rs | 21 +++++++++++---------- src/io_helpers/mod.rs | 8 ++++---- src/routers/middleware_router.rs | 11 +++++++---- src/routers/router.rs | 12 ++++++++---- src/routers/web_socket_router.rs | 4 +--- src/server.rs | 14 ++++++++++---- 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/executors/mod.rs b/src/executors/mod.rs index 440d7ccc1..baec61a85 100644 --- a/src/executors/mod.rs +++ b/src/executors/mod.rs @@ -87,7 +87,7 @@ pub async fn execute_middleware_function<'a>( let res = Python::with_gil(|py| -> PyResult>> { let output: Vec>> = - output.extract(py).unwrap(); + output.extract(py)?; let responses = output[0].clone(); Ok(responses) })?; @@ -113,13 +113,12 @@ pub async fn execute_middleware_function<'a>( 2_u8..=u8::MAX => handler.call1((request,)), }; - let output: Vec>> = - output?.extract().unwrap(); + let output: Vec>> = output?.extract()?; Ok(output[0].clone()) }); - Ok(output.unwrap()) + Ok(output?) } } } @@ -197,7 +196,7 @@ pub async fn execute_http_function( if response_type == "static_file" { let file_path = res.get("file_path").unwrap(); - let contents = read_file(file_path); + let contents = read_file(file_path).unwrap(); res.insert("body".to_owned(), contents); } Ok(res) @@ -235,14 +234,15 @@ pub async fn execute_http_function( pub async fn execute_event_handler( event_handler: Option>, event_loop: Arc>, -) { +) -> Result<(), Box> { if let Some(handler) = event_handler { match &(*handler) { PyFunction::SyncFunction(function) => { println!("Startup event handler"); - Python::with_gil(|py| { - function.call0(py).unwrap(); - }); + Python::with_gil(|py| -> Result<(), Box> { + function.call0(py)?; + Ok(()) + })?; } PyFunction::CoRoutine(function) => { let future = Python::with_gil(|py| { @@ -252,8 +252,9 @@ pub async fn execute_event_handler( pyo3_asyncio::into_future_with_loop((*event_loop).as_ref(py), coroutine) .unwrap() }); - future.await.unwrap(); + future.await?; } } } + Ok(()) } diff --git a/src/io_helpers/mod.rs b/src/io_helpers/mod.rs index 0fb5169da..6a76cc071 100644 --- a/src/io_helpers/mod.rs +++ b/src/io_helpers/mod.rs @@ -21,9 +21,9 @@ pub fn apply_headers(response: &mut HttpResponseBuilder, headers: HashMap String { - let mut file = File::open(file_path).unwrap(); +pub fn read_file(file_path: &str) -> Result> { + let mut file = File::open(file_path)?; let mut buf = vec![]; - file.read_to_end(&mut buf).unwrap(); - String::from_utf8_lossy(&buf).to_string() + file.read_to_end(&mut buf)?; + Ok(String::from_utf8_lossy(&buf).to_string()) } diff --git a/src/routers/middleware_router.rs b/src/routers/middleware_router.rs index 111727f99..1f609f818 100644 --- a/src/routers/middleware_router.rs +++ b/src/routers/middleware_router.rs @@ -7,6 +7,8 @@ use pyo3::types::PyAny; use matchit::Node; +use anyhow::{bail, Error, Result}; + /// Contains the thread safe hashmaps of different routes pub struct MiddlewareRouter { @@ -40,10 +42,10 @@ impl MiddlewareRouter { handler: Py, is_async: bool, number_of_params: u8, - ) { + ) -> Result<(), Error> { let table = match self.get_relevant_map(route_type) { Some(table) => table, - None => return, + None => bail!("No relevant map"), }; let function = if is_async { @@ -55,8 +57,9 @@ impl MiddlewareRouter { table .write() .unwrap() - .insert(route.to_string(), (function, number_of_params)) - .unwrap(); + .insert(route.to_string(), (function, number_of_params))?; + + Ok(()) } pub fn get_route( diff --git a/src/routers/router.rs b/src/routers/router.rs index 01a95c97b..b75aef09d 100644 --- a/src/routers/router.rs +++ b/src/routers/router.rs @@ -8,6 +8,8 @@ use pyo3::types::PyAny; use actix_web::http::Method; use matchit::Node; +use anyhow::{bail, Error, Result}; + /// Contains the thread safe hashmaps of different routes pub struct Router { @@ -76,10 +78,10 @@ impl Router { handler: Py, is_async: bool, number_of_params: u8, - ) { + ) -> Result<(), Error> { let table = match self.get_relevant_map_str(route_type) { Some(table) => table, - None => return, + None => bail!("No relevant map"), }; let function = if is_async { @@ -88,11 +90,13 @@ impl Router { PyFunction::SyncFunction(handler) }; + // try removing unwrap here table .write() .unwrap() - .insert(route.to_string(), (function, number_of_params)) - .unwrap(); + .insert(route.to_string(), (function, number_of_params))?; + + Ok(()) } // Checks if the functions is an async function diff --git a/src/routers/web_socket_router.rs b/src/routers/web_socket_router.rs index 2596f98da..4225d72fc 100644 --- a/src/routers/web_socket_router.rs +++ b/src/routers/web_socket_router.rs @@ -21,9 +21,7 @@ impl WebSocketRouter { } #[inline] - pub fn get_web_socket_map( - &self, - ) -> &WebSocketRoutes { + pub fn get_web_socket_map(&self) -> &WebSocketRoutes { &self.web_socket_routes } diff --git a/src/server.rs b/src/server.rs index 548fdfa39..5c11eba30 100644 --- a/src/server.rs +++ b/src/server.rs @@ -104,7 +104,9 @@ impl Server { let copied_event_loop = event_loop_hdl.clone(); actix_web::rt::System::new().block_on(async move { println!("The number of workers are {}", workers.clone()); - execute_event_handler(startup_handler, copied_event_loop.clone()).await; + execute_event_handler(startup_handler, copied_event_loop.clone()) + .await + .unwrap(); HttpServer::new(move || { let mut app = App::new(); @@ -194,7 +196,9 @@ impl Server { Python::with_gil(|py| { let event_loop_hdl = event_loop_cleanup.clone(); pyo3_asyncio::tokio::run(py, async move { - execute_event_handler(shutdown_handler, event_loop_hdl.clone()).await; + execute_event_handler(shutdown_handler, event_loop_hdl.clone()) + .await + .unwrap(); Ok(()) }) .unwrap(); @@ -242,7 +246,8 @@ impl Server { ) { println!("Route added for {} {} ", route_type, route); self.router - .add_route(route_type, route, handler, is_async, number_of_params); + .add_route(route_type, route, handler, is_async, number_of_params) + .unwrap(); } /// Add a new route to the routing tables @@ -257,7 +262,8 @@ impl Server { ) { println!("MiddleWare Route added for {} {} ", route_type, route); self.middleware_router - .add_route(route_type, route, handler, is_async, number_of_params); + .add_route(route_type, route, handler, is_async, number_of_params) + .unwrap(); } /// Add a new web socket route to the routing tables