Skip to content

Commit

Permalink
Removing unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed May 21, 2022
1 parent c3fdc34 commit 610f45b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
19 changes: 10 additions & 9 deletions src/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub async fn execute_middleware_function<'a>(
let res =
Python::with_gil(|py| -> PyResult<HashMap<String, HashMap<String, String>>> {
let output: Vec<HashMap<String, HashMap<String, String>>> =
output.extract(py).unwrap();
output.extract(py)?;
let responses = output[0].clone();
Ok(responses)
})?;
Expand All @@ -113,13 +113,12 @@ pub async fn execute_middleware_function<'a>(
2_u8..=u8::MAX => handler.call1((request,)),
};

let output: Vec<HashMap<String, HashMap<String, String>>> =
output?.extract().unwrap();
let output: Vec<HashMap<String, HashMap<String, String>>> = output?.extract()?;

Ok(output[0].clone())
});

Ok(output.unwrap())
Ok(output?)
}
}
}
Expand Down Expand Up @@ -235,14 +234,15 @@ pub async fn execute_http_function(
pub async fn execute_event_handler(
event_handler: Option<Arc<PyFunction>>,
event_loop: Arc<Py<PyAny>>,
) {
) -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
function.call0(py)?;
Ok(())
})?;
}
PyFunction::CoRoutine(function) => {
let future = Python::with_gil(|py| {
Expand All @@ -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(())
}
12 changes: 8 additions & 4 deletions src/routers/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,10 +78,10 @@ impl Router {
handler: Py<PyAny>,
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 {
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 610f45b

Please sign in to comment.