Replace expect usage with Result
This commit is contained in:
		
							parent
							
								
									9394a1ae52
								
							
						
					
					
						commit
						b3228ea6b5
					
				
					 11 changed files with 135 additions and 95 deletions
				
			
		| 
						 | 
				
			
			@ -116,14 +116,17 @@ impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
 | 
			
		|||
					let action_res = self.handle_action(&mut pool_conn, ctx, action);
 | 
			
		||||
					if let Err(e) = action_res {
 | 
			
		||||
						log::error!("Error handling action: {:?}", e);
 | 
			
		||||
						ctx.text(serde_json::to_string(&e).expect("Failed to serialize error"));
 | 
			
		||||
						ctx.text(
 | 
			
		||||
							serde_json::to_string(&e)
 | 
			
		||||
								.unwrap_or(format!("Error in handling action: {:?}", e)),
 | 
			
		||||
						);
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				Err(e) => {
 | 
			
		||||
					log::error!("Error deserializing action: {:?}", e);
 | 
			
		||||
					ctx.text(
 | 
			
		||||
						serde_json::to_string(&EmgauwaError::Serialization(e))
 | 
			
		||||
							.expect("Failed to serialize error"),
 | 
			
		||||
							.unwrap_or(String::from("Error in deserializing action")),
 | 
			
		||||
					);
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,6 +5,7 @@ use actix_cors::Cors;
 | 
			
		|||
use actix_web::middleware::TrailingSlash;
 | 
			
		||||
use actix_web::{middleware, web, App, HttpServer};
 | 
			
		||||
use emgauwa_lib::db::DbController;
 | 
			
		||||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::utils::init_logging;
 | 
			
		||||
 | 
			
		||||
use crate::app_state::AppServer;
 | 
			
		||||
| 
						 | 
				
			
			@ -16,27 +17,21 @@ mod settings;
 | 
			
		|||
mod utils;
 | 
			
		||||
 | 
			
		||||
#[actix_web::main]
 | 
			
		||||
async fn main() -> std::io::Result<()> {
 | 
			
		||||
	let settings = settings::init();
 | 
			
		||||
	init_logging(&settings.logging.level);
 | 
			
		||||
async fn main() -> Result<(), std::io::Error> {
 | 
			
		||||
	let settings = settings::init()?;
 | 
			
		||||
	init_logging(&settings.logging.level)?;
 | 
			
		||||
 | 
			
		||||
	let listener = TcpListener::bind(format!("{}:{}", settings.host, settings.port))
 | 
			
		||||
		.expect("Error creating listener");
 | 
			
		||||
	let listener = TcpListener::bind(format!("{}:{}", settings.host, settings.port))?;
 | 
			
		||||
 | 
			
		||||
	drop_privileges(&settings).expect("Error dropping privileges");
 | 
			
		||||
	drop_privileges(&settings)?;
 | 
			
		||||
 | 
			
		||||
	let pool = emgauwa_lib::db::init(&settings.database).await;
 | 
			
		||||
	let pool = emgauwa_lib::db::init(&settings.database).await?;
 | 
			
		||||
 | 
			
		||||
	// This block is to ensure that the connection is dropped after use.
 | 
			
		||||
	{
 | 
			
		||||
		let mut conn = pool
 | 
			
		||||
			.acquire()
 | 
			
		||||
			.await
 | 
			
		||||
			.expect("Failed to get database connection");
 | 
			
		||||
		DbController::all_inactive(&mut conn)
 | 
			
		||||
			.await
 | 
			
		||||
			.expect("Error setting all controllers inactive");
 | 
			
		||||
	}
 | 
			
		||||
	let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?;
 | 
			
		||||
	DbController::all_inactive(&mut conn)
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(EmgauwaError::from)?;
 | 
			
		||||
	conn.close().await.map_err(EmgauwaError::from)?;
 | 
			
		||||
 | 
			
		||||
	let app_server = AppServer::new(pool.clone()).start();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
use emgauwa_lib::errors::EmgauwaError;
 | 
			
		||||
use emgauwa_lib::{constants, utils};
 | 
			
		||||
use serde_derive::Deserialize;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -51,6 +52,6 @@ impl Default for Logging {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn init() -> Settings {
 | 
			
		||||
pub fn init() -> Result<Settings, EmgauwaError> {
 | 
			
		||||
	utils::load_settings("core", "CORE")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue