1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-11-21 11:06:32 +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 fn establish_connection_pool() -> Result<DBPool, String> {
let database_url = match env::var("DATABASE_URL") {
Ok(url) => url,
Err(_) => return Err("Environment variable DATABASE_URL missing!".to_string()),
};
pub fn establish_connection_pool() -> DBPool {
let database_url =
env::var("DATABASE_URL").expect("Environment variable DATABASE_URL missing!");
let manager = ConnectionManager::<SqliteConnection>::new(&database_url);
match Pool::builder().build(manager) {
Ok(pool) => Ok(pool),
Err(_) => Err("Could not build database connection pool!".to_string()),
}
Pool::builder()
.build(manager)
.expect("Could not build database connection pool!")
}
fn get_conn(
pool: &DBPool,
) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>, String> {
match pool.get() {
Ok(pool) => Ok(pool),
Err(_) => Err("Could not get database pool!".to_string()),
}
pool.get()
.or(Err("Could not get database pool!".to_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,
command_with_args: &command_with_args,
current_dir: &hook.current_dir,
stdout: match std::str::from_utf8(&output.stdout) {
Ok(s) => s,
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()),
},
stdout: std::str::from_utf8(&output.stdout).unwrap_or("Could not convert stdout to str!"),
stderr: std::str::from_utf8(&output.stderr).unwrap_or("Could not convert stderr to str!"),
status_code: output.status.code(),
};
let result = diesel::insert_into(hooklog::table)
match diesel::insert_into(hooklog::table)
.values(&new_hook_log)
.get_result::<HookLog>(conn);
match result {
.get_result::<HookLog>(conn)
{
Ok(hook_log) => Ok(hook_log.id),
Err(e) => Err(e.to_string()),
}
@ -79,8 +67,5 @@ pub fn get_hook_log(pool: &DBPool, id: i32) -> Result<HookLog, String> {
.first(conn);
}
match hl {
Ok(hl) => Ok(hl),
Err(e) => Err(e.to_string()),
}
hl.or_else(|e| Err(e.to_string()))
}

View file

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

View file

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