1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-10-18 07:22:39 +00:00

Learning error handling

This commit is contained in:
Mo 2022-10-22 19:18:02 +02:00
parent c270cd91db
commit 5b1b2108bf
4 changed files with 31 additions and 47 deletions

View file

@ -1 +1 @@
= Gitea Webhook = Git Webhook Client

View file

@ -10,26 +10,21 @@ use crate::schema::hooklog;
pub type DBPool = Pool<ConnectionManager<SqliteConnection>>; pub type DBPool = Pool<ConnectionManager<SqliteConnection>>;
pub fn establish_connection_pool() -> Result<DBPool, String> { pub fn establish_connection_pool() -> DBPool {
let database_url = match env::var("DATABASE_URL") { let database_url =
Ok(url) => url, env::var("DATABASE_URL").expect("Environment variable DATABASE_URL missing!");
Err(_) => return Err("Environment variable DATABASE_URL missing!".to_string()),
};
let manager = ConnectionManager::<SqliteConnection>::new(&database_url); let manager = ConnectionManager::<SqliteConnection>::new(&database_url);
match Pool::builder().build(manager) { Pool::builder()
Ok(pool) => Ok(pool), .build(manager)
Err(_) => Err("Could not build database connection pool!".to_string()), .expect("Could not build database connection pool!")
}
} }
fn get_conn( fn get_conn(
pool: &DBPool, pool: &DBPool,
) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>, String> { ) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>, String> {
match pool.get() { pool.get()
Ok(pool) => Ok(pool), .or(Err("Could not get database pool!".to_string()))
Err(_) => Err("Could not get database pool!".to_string()),
}
} }
pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> Result<i32, String> { pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> Result<i32, String> {
@ -42,22 +37,15 @@ pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> Result<i32,
repo_url: &hook.repo_url, repo_url: &hook.repo_url,
command_with_args: &command_with_args, command_with_args: &command_with_args,
current_dir: &hook.current_dir, current_dir: &hook.current_dir,
stdout: match std::str::from_utf8(&output.stdout) { stdout: std::str::from_utf8(&output.stdout).unwrap_or("Could not convert stdout to str!"),
Ok(s) => s, stderr: std::str::from_utf8(&output.stderr).unwrap_or("Could not convert stderr to str!"),
Err(_) => return Err("Can not convert stdout to str!".to_string()),
},
stderr: match std::str::from_utf8(&output.stderr) {
Ok(s) => s,
Err(_) => return Err("Can not convert stderr to str!".to_string()),
},
status_code: output.status.code(), status_code: output.status.code(),
}; };
let result = diesel::insert_into(hooklog::table) match diesel::insert_into(hooklog::table)
.values(&new_hook_log) .values(&new_hook_log)
.get_result::<HookLog>(conn); .get_result::<HookLog>(conn)
{
match result {
Ok(hook_log) => Ok(hook_log.id), Ok(hook_log) => Ok(hook_log.id),
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),
} }
@ -79,8 +67,5 @@ pub fn get_hook_log(pool: &DBPool, id: i32) -> Result<HookLog, String> {
.first(conn); .first(conn);
} }
match hl { hl.or_else(|e| Err(e.to_string()))
Ok(hl) => Ok(hl),
Err(e) => Err(e.to_string()),
}
} }

View file

@ -7,23 +7,27 @@ use crate::db;
use crate::guards; use crate::guards;
use crate::states; use crate::states;
fn bad_req<E>(err: E) -> BadRequest<String>
where
E: std::fmt::Display,
{
BadRequest(Some(err.to_string()))
}
#[get("/?<id>")] #[get("/?<id>")]
pub fn index( pub fn index(
db_state: &State<states::DB>, db_state: &State<states::DB>,
id: Option<i32>, id: Option<i32>,
) -> Result<Template, BadRequest<String>> { ) -> Result<Template, BadRequest<String>> {
let id = match id { let id = id.unwrap_or(-1);
Some(id) => id,
None => -1,
};
if id == 0 { if id == 0 {
return Err(BadRequest(Some("id=0 not allowed!".to_string()))); return Err(bad_req("id=0 not allowed!"));
} }
let hook_log = match db::get_hook_log(&db_state.pool, id) { let hook_log = match db::get_hook_log(&db_state.pool, id) {
Ok(hl) => hl, Ok(hl) => hl,
Err(e) => return Err(BadRequest(Some(e))), Err(e) => return Err(bad_req(e)),
}; };
Ok(Template::render("hook_log", hook_log)) Ok(Template::render("hook_log", hook_log))
@ -38,10 +42,10 @@ pub fn trigger(
let hook = match config_state.get_hook(repo.clone_url) { let hook = match config_state.get_hook(repo.clone_url) {
Some(hook) => hook, Some(hook) => hook,
None => { None => {
return Err(BadRequest(Some(format!( return Err(bad_req(format!(
"No matching repository with url {} in the configuration file.", "No matching repository with url {} in the configuration file.",
repo.clone_url repo.clone_url
)))) )))
} }
}; };
@ -51,15 +55,11 @@ pub fn trigger(
.output() .output()
{ {
Ok(output) => output, Ok(output) => output,
Err(_) => { Err(_) => return Err(bad_req("Can not run the hook command!")),
return Err(BadRequest(Some(
"Can not run the hook command!".to_string(),
)))
}
}; };
match db::add_hook_log(&db_state.pool, hook, &output) { match db::add_hook_log(&db_state.pool, hook, &output) {
Ok(new_hook_log_id) => Ok(format!("{}/?id={}", config_state.base_url, new_hook_log_id)), Ok(new_hook_log_id) => Ok(format!("{}/?id={}", config_state.base_url, new_hook_log_id)),
Err(e) => Err(BadRequest(Some(e))), Err(e) => Err(bad_req(e)),
} }
} }

View file

@ -7,9 +7,8 @@ pub struct DB {
impl DB { impl DB {
pub fn new() -> Self { pub fn new() -> Self {
match db::establish_connection_pool() { Self {
Ok(pool) => Self { pool }, pool: db::establish_connection_pool(),
Err(e) => panic!("Could not establish database pool: {}", e),
} }
} }
} }