From 45c0af4d1e35a654b3da58efc6ba318fb40d119a Mon Sep 17 00:00:00 2001 From: "Stephen McQuay (smcquay)" Date: Tue, 21 Jun 2016 07:54:00 -0700 Subject: [PATCH] Better error reporting Change-Id: I36dc5ad3648c5b4ac2cc2c72ba90fec7398090bf --- api_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++------ db.go | 2 +- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/api_test.go b/api_test.go index 60113ae..2aeb582 100644 --- a/api_test.go +++ b/api_test.go @@ -631,16 +631,28 @@ func TestRoundTrip(t *testing.T) { u := fmt.Sprintf("%s%s?email=fake@example.com", ts.URL, prefix["register"]) req, err := http.NewRequest("POST", u, nil) - _, err = http.DefaultClient.Do(req) - if err != nil { - t.Fatalf("couldn't POST: %v", err) - } - - req, err = http.NewRequest("GET", mm.msg, nil) resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatalf("couldn't POST: %v", err) } + if got, want := resp.StatusCode, http.StatusOK; got != want { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) + t.Fatalf("bad request got incorrect status: got %d, want %d", got, want) + } + + req, err = http.NewRequest("GET", mm.msg, nil) + resp, err = http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("couldn't POST: %v", err) + } + if got, want := resp.StatusCode, http.StatusOK; got != want { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) + t.Fatalf("bad request got incorrect status: got %d, want %d", got, want) + } _, err = db.user("fake@example.com") if err != nil { @@ -661,6 +673,12 @@ func TestRoundTrip(t *testing.T) { if err != nil { t.Fatalf("couldn't POST: %v", err) } + if got, want := resp.StatusCode, http.StatusOK; got != want { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) + t.Fatalf("bad request got incorrect status: got %d, want %d", got, want) + } if got, want := len(db.Pkgs()), 1; got != want { t.Fatalf("pkgs should have something in it; got %d, want %d", got, want) @@ -687,6 +705,9 @@ func TestForgot(t *testing.T) { t.Fatalf("couldn't POST: %v", err) } if status := resp.StatusCode; status != http.StatusNotFound { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) t.Fatalf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest) } @@ -698,6 +719,9 @@ func TestForgot(t *testing.T) { t.Fatalf("couldn't POST: %v", err) } if status := resp.StatusCode; status != http.StatusBadRequest { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) t.Fatalf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest) } @@ -714,6 +738,12 @@ func TestForgot(t *testing.T) { if err != nil { t.Fatalf("couldn't POST: %v", err) } + if got, want := resp.StatusCode, http.StatusOK; got != want { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) + t.Fatalf("bad request got incorrect status: got %d, want %d", got, want) + } //check database for new user _, err = db.user("fake@example.com") @@ -738,6 +768,12 @@ func TestForgot(t *testing.T) { if err != nil { t.Fatalf("couldn't POST: %v", err) } + if got, want := resp.StatusCode, http.StatusOK; got != want { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) + t.Fatalf("bad request got incorrect status: got %d, want %d", got, want) + } ft, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatalf("Failed to parse response body: %v", err) @@ -758,6 +794,12 @@ func TestForgot(t *testing.T) { if err != nil { t.Fatalf("couldn't POST: %v", err) } + if got, want := resp.StatusCode, http.StatusOK; got != want { + buf := &bytes.Buffer{} + io.Copy(buf, resp.Body) + t.Logf("%s", buf.Bytes()) + t.Fatalf("bad request got incorrect status: got %d, want %d", got, want) + } if got, want := len(db.Pkgs()), 1; got != want { t.Fatalf("pkgs should have something in it; got %d, want %d", got, want) } diff --git a/db.go b/db.go index 2f7c99a..fc23365 100644 --- a/db.go +++ b/db.go @@ -259,7 +259,7 @@ func (db *DB) Confirm(token string) (string, error) { ) if err != nil { return "", verrors.HTTP{ - Message: fmt.Sprintf("couldn't update user with token %q", token), + Message: fmt.Sprintf("couldn't update user with token %q: %v", token, err), Code: http.StatusInternalServerError, } }