From f7e20d769f4c014725fb4eb2dba7059abe51d78e Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:37:10 +0200 Subject: [PATCH] errcheck --- common_test.go | 6 +++++- mailgun_test.go | 3 ++- messages_test.go | 27 ++++++++++++++++++--------- webhooks_test.go | 6 +++--- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/common_test.go b/common_test.go index 5c4481e8..0867f854 100644 --- a/common_test.go +++ b/common_test.go @@ -13,7 +13,11 @@ import ( func randomString(n int, prefix string) string { const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" var bytes = make([]byte, n) - rand.Read(bytes) + _, err := rand.Read(bytes) + if err != nil { + panic(fmt.Sprintf("rand.Read: %v", err)) + } + for i, b := range bytes { bytes[i] = alphanum[b%byte(len(alphanum))] } diff --git a/mailgun_test.go b/mailgun_test.go index 3f0d0970..f4e11c74 100644 --- a/mailgun_test.go +++ b/mailgun_test.go @@ -43,7 +43,8 @@ func TestValidBaseAPI(t *testing.T) { b, err := json.Marshal(resp) require.NoError(t, err) - w.Write(b) + _, err = w.Write(b) + require.NoError(t, err) })) apiBases := []string{ diff --git a/messages_test.go b/messages_test.go index 7385c70a..dc0fe650 100644 --- a/messages_test.go +++ b/messages_test.go @@ -116,7 +116,8 @@ func TestSendMGSTO(t *testing.T) { ctx := context.Background() m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) - m.SetSTOPeriod("24h") + err = m.SetSTOPeriod("24h") + require.NoError(t, err) msg, id, err := mg.Send(ctx, m) require.NoError(t, err) t.Log("TestSendMGSTO:MSG(" + msg + "),ID(" + id + ")") @@ -218,9 +219,12 @@ func TestSendMGTag(t *testing.T) { ctx := context.Background() m := mailgun.NewMessage(fromUser, exampleSubject, exampleText+"Tags Galore!\n", toUser) - m.AddTag("FooTag") - m.AddTag("BarTag") - m.AddTag("BlortTag") + err = m.AddTag("FooTag") + require.NoError(t, err) + err = m.AddTag("BarTag") + require.NoError(t, err) + err = m.AddTag("BlortTag") + require.NoError(t, err) msg, id, err := mg.Send(ctx, m) require.NoError(t, err) t.Log("TestSendTag:MSG(" + msg + "),ID(" + id + ")") @@ -255,7 +259,8 @@ func TestSendMGBatchFailRecipients(t *testing.T) { m := mailgun.NewMessage(fromUser, exampleSubject, exampleText+"Batch\n") for i := 0; i < mailgun.MaxNumberOfRecipients; i++ { - m.AddRecipient("") // We expect this to indicate a failure at the API + err := m.AddRecipient("") // We expect this to indicate a failure at the API + require.NoError(t, err) } err := m.AddRecipientAndVariables(toUser, nil) // In case of error the SDK didn't send the message, @@ -397,10 +402,14 @@ func TestSendMGMessageVariables(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) - m.AddVariable(exampleStrVarKey, exampleStrVarVal) - m.AddVariable(exampleBoolVarKey, false) - m.AddVariable(exampleMapVarKey, exampleMapVarVal) - m.AddTemplateVariable("templateVariable", exampleTemplateVariable) + err := m.AddVariable(exampleStrVarKey, exampleStrVarVal) + require.NoError(t, err) + err = m.AddVariable(exampleBoolVarKey, false) + require.NoError(t, err) + err = m.AddVariable(exampleMapVarKey, exampleMapVarVal) + require.NoError(t, err) + err = m.AddTemplateVariable("templateVariable", exampleTemplateVariable) + require.NoError(t, err) msg, id, err := mg.Send(context.Background(), m) require.NoError(t, err) diff --git a/webhooks_test.go b/webhooks_test.go index 90a8ae8a..0dcfcaf2 100644 --- a/webhooks_test.go +++ b/webhooks_test.go @@ -149,7 +149,7 @@ func buildMultipartFormRequest(fields map[string]string) *http.Request { writer := multipart.NewWriter(buf) for k, v := range fields { - writer.WriteField(k, v) + _ = writer.WriteField(k, v) } writer.Close() @@ -171,8 +171,8 @@ func getSignatureFields(key string, signed bool) map[string]string { if signed { h := hmac.New(sha256.New, []byte(key)) - io.WriteString(h, fields["timestamp"]) - io.WriteString(h, fields["token"]) + _, _ = io.WriteString(h, fields["timestamp"]) + _, _ = io.WriteString(h, fields["token"]) hash := h.Sum(nil) fields["signature"] = hex.EncodeToString(hash)