From 18e6a837f3a4cd87ccfc182cf00ae42a1d8e1a9d Mon Sep 17 00:00:00 2001 From: Mo8it Date: Thu, 13 Oct 2022 22:23:15 +0200 Subject: [PATCH] Replace all unwraps with expect --- src/config.rs | 10 ++++++++-- src/db.rs | 22 +++++++++++++++------- src/guards.rs | 26 ++++++++++++++++++++------ src/routes.rs | 7 +++++-- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/config.rs b/src/config.rs index 453b014..8dd04df 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,9 +21,15 @@ pub struct Config { impl Config { pub fn new() -> Self { let config_path = Path::new("config.json"); - let config_file = File::open(config_path).unwrap(); + let config_file = File::open(config_path).expect(&format!( + "Can not open the config file at the path {}!", + config_path + .to_str() + .expect("Can not convert the config file path into a string") + )); let config_reader = BufReader::new(config_file); - let config: Self = serde_json::from_reader(config_reader).unwrap(); + let config: Self = + serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!"); config } diff --git a/src/db.rs b/src/db.rs index deacc54..2ae2044 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,6 +1,6 @@ use chrono::Local; use diesel::prelude::*; -use diesel::r2d2::{ConnectionManager, Pool}; +use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; use std::env; use std::process::Output; @@ -20,8 +20,13 @@ pub fn establish_connection_pool() -> DBPool { .expect("Could not build database connection pool!") } +fn get_conn(pool: &DBPool) -> PooledConnection> { + pool.get() + .expect("Can not get a connection from the database pool!") +} + pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> i32 { - let conn = &mut pool.get().unwrap(); + let conn = &mut get_conn(pool); let command_with_args = hook.command.to_owned() + " " + &hook.args.join(" "); @@ -30,8 +35,8 @@ pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> i32 { repo_url: &hook.repo_url, command_with_args: &command_with_args, current_dir: &hook.current_dir, - stdout: std::str::from_utf8(&output.stdout).unwrap(), - stderr: std::str::from_utf8(&output.stderr).unwrap(), + stdout: std::str::from_utf8(&output.stdout).expect("Can not convert stdout to str!"), + stderr: std::str::from_utf8(&output.stderr).expect("Can not convert stderr to str!"), status_code: output.status.code(), }; @@ -44,15 +49,18 @@ pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> i32 { } pub fn get_hook_log(pool: &DBPool, id: i32) -> HookLog { - let conn = &mut pool.get().unwrap(); + let conn = &mut get_conn(pool); if id >= 0 { - hooklog::dsl::hooklog.find(id).first(conn).unwrap() + hooklog::dsl::hooklog + .find(id) + .first(conn) + .expect("No hook log exists for this id!") } else { hooklog::dsl::hooklog .order(hooklog::dsl::id.desc()) .offset((-id - 1).into()) .first(conn) - .unwrap() + .expect("No hook log exists for this negative id!") } } diff --git a/src/guards.rs b/src/guards.rs index 156883b..80170f2 100644 --- a/src/guards.rs +++ b/src/guards.rs @@ -35,7 +35,9 @@ impl<'r> FromData<'r> for Repo<'r> { let mut received_signatures = req.headers().get("X-GITEA-SIGNATURE"); let received_signature = match received_signatures.next() { - Some(signature) => hex::decode(signature).unwrap(), + Some(signature) => { + hex::decode(signature).expect("Can not hex decode the received signature!") + } None => return Outcome::Failure((Status::BadRequest, Self::Error::MissingSignature)), }; @@ -43,15 +45,26 @@ impl<'r> FromData<'r> for Repo<'r> { return Outcome::Failure((Status::BadRequest, Self::Error::MoreThatOneSignature)); } - let config_state = req.rocket().state::().unwrap(); + let config_state = req + .rocket() + .state::() + .expect("Can not get the config state!"); if !is_valid_signature(&config_state.secret, &received_signature, &payload) { return Outcome::Failure((Status::BadRequest, Self::Error::InvalidSignature)); } - let json: Value = serde_json::from_slice(&payload).unwrap(); - let repo = json.get("repository").unwrap(); - let clone_url = repo.get("clone_url").unwrap().as_str().unwrap().to_string(); + let json: Value = + serde_json::from_slice(&payload).expect("Can not parse payload into JSON!"); + let repo = json + .get("repository") + .expect("Can not get the repository value from the payload!"); + let clone_url = repo + .get("clone_url") + .expect("Can not get value clone_url from repository in the payload!") + .as_str() + .expect("The value of clone_url from repository in the payload is not a string!") + .to_string(); let clone_url = request::local_cache!(req, clone_url); @@ -60,7 +73,8 @@ impl<'r> FromData<'r> for Repo<'r> { } fn is_valid_signature(secret: &[u8], received_signature: &[u8], payload: &[u8]) -> bool { - let mut mac = Hmac::::new_from_slice(secret).unwrap(); + let mut mac = + Hmac::::new_from_slice(secret).expect("Can not generate a mac from the secret!"); mac.update(payload); let expected_signature = mac.finalize().into_bytes(); diff --git a/src/routes.rs b/src/routes.rs index 7f550ac..e42c837 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -59,13 +59,16 @@ pub fn trigger( config_state: &State, repo: guards::Repo, ) -> String { - let hook = config_state.get_hook(repo.clone_url).unwrap(); + let hook = config_state.get_hook(repo.clone_url).expect(&format!( + "No matching repository with url {} in the configuration file.", + repo.clone_url + )); let output = Command::new(&hook.command) .args(&hook.args) .current_dir(&hook.current_dir) .output() - .unwrap(); + .expect("Can not run the hook command!"); let new_hook_log_id = db::add_hook_log(&db_state.pool, hook, &output);