Skip to content

Commit 479c053

Browse files
committed
Fix golangci-lint errcheck warnings
Handle unchecked error returns in CLN backend and tests: - Wrap resp.Body.Close() in defer functions to acknowledge errors - Add error checks for json.Decoder.Decode() calls - Ignore non-critical errors with _ where appropriate (json.Encoder.Encode, w.Write in tests) Signed-off-by: Gustavo Chain <me@qustavo.cc>
1 parent cc67e22 commit 479c053

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

pkg/lightning/cln.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (b *CLNBackend) post(ctx context.Context, path string, body any) (*http.Res
8787
}
8888

8989
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
90-
defer resp.Body.Close()
90+
defer func() { _ = resp.Body.Close() }()
9191
body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
9292
return nil, fmt.Errorf("POST %s: HTTP %d: %s", path, resp.StatusCode, string(body))
9393
}
@@ -103,17 +103,16 @@ func (b *CLNBackend) Wait(ctx context.Context) error {
103103
if err != nil {
104104
return fmt.Errorf("getinfo: %w", err)
105105
}
106+
defer func() { _ = resp.Body.Close() }()
106107

107108
var info struct {
108109
WarningBitcoindSync string `json:"warning_bitcoind_sync"`
109110
WarningLightningdSync string `json:"warning_lightningd_sync"`
110111
BlockHeight int64 `json:"blockheight"`
111112
}
112113
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
113-
resp.Body.Close()
114114
return fmt.Errorf("decoding getinfo: %w", err)
115115
}
116-
resp.Body.Close()
117116

118117
// Synced when both warnings are absent/empty
119118
if info.WarningBitcoindSync == "" && info.WarningLightningdSync == "" {
@@ -151,7 +150,7 @@ func (b *CLNBackend) CreateInvoice(ctx context.Context, amountMsat int64, memo s
151150
if err != nil {
152151
return nil, fmt.Errorf("invoice: %w", err)
153152
}
154-
defer resp.Body.Close()
153+
defer func() { _ = resp.Body.Close() }()
155154

156155
var invResp struct {
157156
PaymentHash string `json:"payment_hash"`
@@ -178,7 +177,7 @@ func (b *CLNBackend) VerifyPayment(ctx context.Context, paymentHash string) (boo
178177
if err != nil {
179178
return false, fmt.Errorf("listinvoices: %w", err)
180179
}
181-
defer resp.Body.Close()
180+
defer func() { _ = resp.Body.Close() }()
182181

183182
var listResp struct {
184183
Invoices []struct {

pkg/lightning/cln_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestCLNBackend_Wait_NotSynced(t *testing.T) {
1515
w.WriteHeader(http.StatusBadRequest)
1616
return
1717
}
18-
json.NewEncoder(w).Encode(map[string]interface{}{
18+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
1919
"warning_bitcoind_sync": "syncing blocks",
2020
"warning_lightningd_sync": "",
2121
"blockheight": 100,
@@ -47,13 +47,13 @@ func TestCLNBackend_Wait_Synced(t *testing.T) {
4747
}
4848
callCount++
4949
if callCount < 2 {
50-
json.NewEncoder(w).Encode(map[string]interface{}{
50+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
5151
"warning_bitcoind_sync": "",
5252
"warning_lightningd_sync": "syncing",
5353
"blockheight": 100,
5454
})
5555
} else {
56-
json.NewEncoder(w).Encode(map[string]interface{}{
56+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
5757
"warning_bitcoind_sync": "",
5858
"warning_lightningd_sync": "",
5959
"blockheight": 101,
@@ -87,14 +87,17 @@ func TestCLNBackend_CreateInvoice(t *testing.T) {
8787
}
8888

8989
var req map[string]interface{}
90-
json.NewDecoder(r.Body).Decode(&req)
90+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
91+
w.WriteHeader(http.StatusBadRequest)
92+
return
93+
}
9194

9295
if req["amount_msat"].(float64) != 5000 || req["description"].(string) != "test memo" {
9396
w.WriteHeader(http.StatusBadRequest)
9497
return
9598
}
9699

97-
json.NewEncoder(w).Encode(map[string]string{
100+
_ = json.NewEncoder(w).Encode(map[string]string{
98101
"payment_hash": "abcd1234567890ef",
99102
"bolt11": "lnbc50u1p...",
100103
})
@@ -128,7 +131,7 @@ func TestCLNBackend_VerifyPayment_Unpaid(t *testing.T) {
128131
w.WriteHeader(http.StatusBadRequest)
129132
return
130133
}
131-
json.NewEncoder(w).Encode(map[string]interface{}{
134+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
132135
"invoices": []map[string]string{
133136
{"status": "unpaid"},
134137
},
@@ -157,7 +160,7 @@ func TestCLNBackend_VerifyPayment_Paid(t *testing.T) {
157160
w.WriteHeader(http.StatusBadRequest)
158161
return
159162
}
160-
json.NewEncoder(w).Encode(map[string]interface{}{
163+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
161164
"invoices": []map[string]string{
162165
{"status": "paid"},
163166
},
@@ -186,7 +189,7 @@ func TestCLNBackend_VerifyPayment_NotFound(t *testing.T) {
186189
w.WriteHeader(http.StatusBadRequest)
187190
return
188191
}
189-
json.NewEncoder(w).Encode(map[string]interface{}{
192+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
190193
"invoices": []map[string]string{},
191194
})
192195
}))
@@ -210,7 +213,7 @@ func TestCLNBackend_VerifyPayment_NotFound(t *testing.T) {
210213
func TestCLNBackend_HTTPError(t *testing.T) {
211214
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
212215
w.WriteHeader(http.StatusUnauthorized)
213-
w.Write([]byte("Invalid rune"))
216+
_, _ = w.Write([]byte("Invalid rune"))
214217
}))
215218
defer server.Close()
216219

0 commit comments

Comments
 (0)