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 8898b6f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 29 deletions.
21 changes: 11 additions & 10 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 @@ -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)
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(())
}
8 changes: 4 additions & 4 deletions src/io_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub fn apply_headers(response: &mut HttpResponseBuilder, headers: HashMap<String
/// * `file_path` - The file path that we want the function to read
///
// ideally this should be async
pub fn read_file(file_path: &str) -> String {
let mut file = File::open(file_path).unwrap();
pub fn read_file(file_path: &str) -> Result<String, Box<dyn std::error::Error>> {
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())
}
11 changes: 7 additions & 4 deletions src/routers/middleware_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -40,10 +42,10 @@ impl MiddlewareRouter {
handler: Py<PyAny>,
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 {
Expand All @@ -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(
Expand Down
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
4 changes: 1 addition & 3 deletions src/routers/web_socket_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 10 additions & 4 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 All @@ -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
Expand Down

0 comments on commit 8898b6f

Please sign in to comment.