From 01f52e557e82545212b3739a3e75fe85168b2436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Sat, 25 Nov 2023 12:03:20 +0100 Subject: [PATCH] Harden code against potential nil panics Detected using go.uber.org/nilaway/cmd/nilaway --- post.go | 2 +- post_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/post.go b/post.go index a027d2a..cc72da9 100644 --- a/post.go +++ b/post.go @@ -29,7 +29,7 @@ func (c *Client) do(ctx context.Context, form url.Values) ([]byte, error) { req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := c.httpClient().Do(req) - if err != nil { + if (err != nil) || (resp == nil) { return nil, fmt.Errorf("failed to contact API: %w", err) } diff --git a/post_test.go b/post_test.go index 66d1b89..47a06aa 100644 --- a/post_test.go +++ b/post_test.go @@ -135,7 +135,7 @@ func TestUpdateOK(t *testing.T) { _, err := client.Update(ctx, records) if err != nil { var netErr net.Error - if errors.As(err, &netErr) && netErr.Timeout() { + if errors.As(err, &netErr) && (netErr != nil) && netErr.Timeout() { t.Errorf("Timeoutsss: %s", netErr) } else { t.Errorf("Successful post should return OK but failed with error: %s", err)