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(-) 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 From 7f19527d1a89b44ae559861c5f33cf126c3b011c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Jun 2017 22:17:50 +0200 Subject: Update TODO --- TODO | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index 08cdf7b..2065140 100644 --- a/TODO +++ b/TODO @@ -7,7 +7,8 @@ v Command line arguments: (2017.06.03) v Date (optional, format: 2017-01-31) v Description (optional) -- Handle failing responses from the server (show errors to the user) +v Handle failing responses from the server (show errors to the user) + (2017.06.03) v Config (2017.06.03) v A `--write-config` or similar option that generates and write a bare config for users to use -- cgit v1.2.3