From 8e7fb7199f1e1f84c914bbaa00fa057e3489f1e8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Jun 2017 22:13:30 +0200 Subject: main(): Handle errors from HTTP responses Add some simple error handling for known responses from our TimeTask HTTP requests. Check a couple of known strings to determine whether there was an error. If so, exit with a failing error code. Remove our old `log` statements. These were used during debugging to see some output and check responses from TimeTask. We now have an idea of what those responses are, and are handling some of the error cases. Thus the log statements are no longer needed. --- main.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 3b7ace5..861dc8b 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,8 @@ package main import ( "fmt" "io/ioutil" - "log" "os" + "strings" "time" "github.com/teddywing/timetasker/timetask" @@ -97,17 +97,27 @@ func main() { password, ) kingpin.FatalIfError(err, "login request failed") - log.Printf("%+v\n", resp) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) - log.Println(string(body)) + if strings.Contains( + string(body), + "The username and password don't appear to be valid.", + ) { + kingpin.Errorf("TimeTask authentication failed") + os.Exit(1) + } resp, err = timetask.SubmitTimeEntry(*client, time_entry) kingpin.FatalIfError(err, "time entry submission request failed") - log.Printf("%+v\n", resp) defer resp.Body.Close() body, err = ioutil.ReadAll(resp.Body) - log.Println(string(body)) + if strings.Contains( + string(body), + "No time entries were created.", + ) { + kingpin.Errorf("time entry creation failed") + os.Exit(1) + } } -- cgit v1.2.3