From 7b62d76be439e11db52933d9f168d6f3ac5691c5 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 11 Nov 2017 22:29:03 +0100 Subject: main(): Handle errors from `Result`s Now that our functions return `Result`s for errors instead of panicking, we need to handle these errors. For now, just respond with a 500. We'll also want to log the errors though. --- src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e5f7235..5882909 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,11 @@ use std::io::Read; use kipper::jenkins; use kipper::pull_request::CommitRef; +fn internal_server_error() -> rouille::Response { + rouille::Response::text("500 Internal Server Error") + .with_status_code(500) +} + fn main() { rouille::start_server("localhost:8000", move |request| { router!(request, @@ -20,9 +25,15 @@ fn main() { Some(mut data) => { try_or_400!(data.read_to_string(&mut body)); - let commit_ref = CommitRef::new(body.as_ref()); + let commit_ref = match CommitRef::new(body.as_ref()) { + Ok(cr) => cr, + Err(_) => return internal_server_error(), + }; - jenkins::find_and_track_build_and_update_status(commit_ref); + match jenkins::find_and_track_build_and_update_status(commit_ref) { + Ok(_) => {}, + Err(_) => return internal_server_error(), + }; rouille::Response::text("202 Accepted") .with_status_code(202) -- cgit v1.2.3