Replace expect usage with Result

This commit is contained in:
Tobias Reisinger 2023-12-05 01:42:19 +01:00
parent 9394a1ae52
commit b3228ea6b5
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
11 changed files with 135 additions and 95 deletions
emgauwa-core/src
handlers/v1/ws/controllers
main.rssettings.rs

View file

@ -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")),
);
}
},

View file

@ -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();

View file

@ -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")
}