Skip to content

Commit f756868

Browse files
author
ffranr
authored
Merge pull request #812 from lightninglabs/lint-err-wrap
Linter checks for `fmt.Errorf` error wrap misuse
2 parents d37b486 + f00697f commit f756868

33 files changed

+111
-105
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ run:
33
deadline: 4m
44

55
linters-settings:
6+
errorlint:
7+
comparison: false
8+
asserts: false
69
govet:
710
# Don't report about shadowed variables
811
check-shadowing: false
@@ -26,6 +29,7 @@ linters-settings:
2629

2730
linters:
2831
enable:
32+
- errorlint
2933
- gofmt
3034
- tagliatelle
3135
- whitespace

cmd/tapcli/addrs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func newAddr(ctx *cli.Context) error {
7171

7272
assetID, err := hex.DecodeString(ctx.String(assetIDName))
7373
if err != nil {
74-
return fmt.Errorf("unable to decode assetID: %v", err)
74+
return fmt.Errorf("unable to decode assetID: %w", err)
7575
}
7676

7777
ctxc := getContext()
@@ -168,7 +168,7 @@ func queryAddr(ctx *cli.Context) error {
168168
Offset: int32(ctx.Int64(offsetName)),
169169
})
170170
if err != nil {
171-
return fmt.Errorf("unable to make addrs: %v", err)
171+
return fmt.Errorf("unable to make addrs: %w", err)
172172
}
173173

174174
printRespJSON(addrs)

cmd/tapcli/cmd_profile.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func profileAdd(ctx *cli.Context) error {
118118
// Create a profile struct from all the global options.
119119
profile, err := profileFromContext(ctx, true, false)
120120
if err != nil {
121-
return fmt.Errorf("could not load global options: %v", err)
121+
return fmt.Errorf("could not load global options: %w", err)
122122
}
123123

124124
// Finally, all that's left is to get the profile name from either
@@ -149,7 +149,7 @@ func profileAdd(ctx *cli.Context) error {
149149
// All done, store the updated profile file.
150150
f.Profiles = append(f.Profiles, profile)
151151
if err = saveProfileFile(defaultProfileFile, f); err != nil {
152-
return fmt.Errorf("error writing profile file %s: %v",
152+
return fmt.Errorf("error writing profile file %s: %w",
153153
defaultProfileFile, err)
154154
}
155155

@@ -180,7 +180,7 @@ func profileRemove(ctx *cli.Context) error {
180180
// Load the default profile file.
181181
f, err := loadProfileFile(defaultProfileFile)
182182
if err != nil {
183-
return fmt.Errorf("could not load profile file: %v", err)
183+
return fmt.Errorf("could not load profile file: %w", err)
184184
}
185185

186186
// Get the profile name from either positional argument or flag.
@@ -255,7 +255,7 @@ func profileSetDefault(ctx *cli.Context) error {
255255
// Load the default profile file.
256256
f, err := loadProfileFile(defaultProfileFile)
257257
if err != nil {
258-
return fmt.Errorf("could not load profile file: %v", err)
258+
return fmt.Errorf("could not load profile file: %w", err)
259259
}
260260

261261
// Get the profile name from either positional argument or flag.
@@ -308,7 +308,7 @@ func profileUnsetDefault(_ *cli.Context) error {
308308
// Load the default profile file.
309309
f, err := loadProfileFile(defaultProfileFile)
310310
if err != nil {
311-
return fmt.Errorf("could not load profile file: %v", err)
311+
return fmt.Errorf("could not load profile file: %w", err)
312312
}
313313

314314
// Save the file with the flag disabled.
@@ -354,7 +354,7 @@ func profileAddMacaroon(ctx *cli.Context) error {
354354
// yet.
355355
f, err := loadProfileFile(defaultProfileFile)
356356
if err != nil {
357-
return fmt.Errorf("could not load profile file: %v", err)
357+
return fmt.Errorf("could not load profile file: %w", err)
358358
}
359359

360360
// Finally, all that's left is to get the profile name from either
@@ -420,25 +420,25 @@ func profileAddMacaroon(ctx *cli.Context) error {
420420
macPath := lncfg.CleanAndExpandPath(ctx.GlobalString("macaroonpath"))
421421
macBytes, err := os.ReadFile(macPath)
422422
if err != nil {
423-
return fmt.Errorf("unable to read macaroon path: %v", err)
423+
return fmt.Errorf("unable to read macaroon path: %w", err)
424424
}
425425
mac := &macaroon.Macaroon{}
426426
if err = mac.UnmarshalBinary(macBytes); err != nil {
427-
return fmt.Errorf("unable to decode macaroon: %v", err)
427+
return fmt.Errorf("unable to decode macaroon: %w", err)
428428
}
429429
macEntry := &macaroonEntry{
430430
Name: macName,
431431
}
432432
if err = macEntry.storeMacaroon(mac, nil); err != nil {
433-
return fmt.Errorf("unable to store macaroon: %v", err)
433+
return fmt.Errorf("unable to store macaroon: %w", err)
434434
}
435435

436436
// All done, store the updated profile file.
437437
selectedProfile.Macaroons.Jar = append(
438438
selectedProfile.Macaroons.Jar, macEntry,
439439
)
440440
if err = saveProfileFile(defaultProfileFile, f); err != nil {
441-
return fmt.Errorf("error writing profile file %s: %v",
441+
return fmt.Errorf("error writing profile file %s: %w",
442442
defaultProfileFile, err)
443443
}
444444

cmd/tapcli/macaroon_jar.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,26 @@ func (e *macaroonEntry) loadMacaroon(
6262
pw, err := pwCallback("Enter macaroon encryption password: ")
6363
if err != nil {
6464
return nil, fmt.Errorf("could not read password from "+
65-
"terminal: %v", err)
65+
"terminal: %w", err)
6666
}
6767

6868
macBytes, err = decryptMacaroon(parts[1], parts[2], pw)
6969
if err != nil {
70-
return nil, fmt.Errorf("unable to decrypt macaroon: %v",
70+
return nil, fmt.Errorf("unable to decrypt macaroon: %w",
7171
err)
7272
}
7373
} else {
7474
macBytes, err = hex.DecodeString(e.Data)
7575
if err != nil {
7676
return nil, fmt.Errorf("unable to hex decode "+
77-
"macaroon: %v", err)
77+
"macaroon: %w", err)
7878
}
7979
}
8080

8181
// Parse the macaroon data into its native struct.
8282
mac := &macaroon.Macaroon{}
8383
if err := mac.UnmarshalBinary(macBytes); err != nil {
84-
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
84+
return nil, fmt.Errorf("unable to decode macaroon: %w", err)
8585
}
8686
return mac, nil
8787
}
@@ -93,7 +93,7 @@ func (e *macaroonEntry) storeMacaroon(mac *macaroon.Macaroon, pw []byte) error {
9393
// First of all, make sure we can serialize the macaroon.
9494
macBytes, err := mac.MarshalBinary()
9595
if err != nil {
96-
return fmt.Errorf("unable to marshal macaroon: %v", err)
96+
return fmt.Errorf("unable to marshal macaroon: %w", err)
9797
}
9898

9999
if len(pw) == 0 {
@@ -106,14 +106,14 @@ func (e *macaroonEntry) storeMacaroon(mac *macaroon.Macaroon, pw []byte) error {
106106
&pw, snacl.DefaultN, snacl.DefaultR, snacl.DefaultP,
107107
)
108108
if err != nil {
109-
return fmt.Errorf("unable to create encryption key: %v", err)
109+
return fmt.Errorf("unable to create encryption key: %w", err)
110110
}
111111

112112
// Encrypt the macaroon data with the derived key and store it in the
113113
// human readable format snacl:<key_base64>:<encrypted_macaroon_base64>.
114114
encryptedMac, err := key.Encrypt(macBytes)
115115
if err != nil {
116-
return fmt.Errorf("unable to encrypt macaroon: %v", err)
116+
return fmt.Errorf("unable to encrypt macaroon: %w", err)
117117
}
118118

119119
keyB64 := base64.StdEncoding.EncodeToString(key.Marshal())
@@ -130,19 +130,19 @@ func decryptMacaroon(keyB64, dataB64 string, pw []byte) ([]byte, error) {
130130
keyData, err := base64.StdEncoding.DecodeString(keyB64)
131131
if err != nil {
132132
return nil, fmt.Errorf("could not base64 decode encryption "+
133-
"key: %v", err)
133+
"key: %w", err)
134134
}
135135
encryptedMac, err := base64.StdEncoding.DecodeString(dataB64)
136136
if err != nil {
137137
return nil, fmt.Errorf("could not base64 decode macaroon "+
138-
"data: %v", err)
138+
"data: %w", err)
139139
}
140140

141141
// Unmarshal the encryption key and ask the user for the password.
142142
key := &snacl.SecretKey{}
143143
err = key.Unmarshal(keyData)
144144
if err != nil {
145-
return nil, fmt.Errorf("could not unmarshal encryption key: %v",
145+
return nil, fmt.Errorf("could not unmarshal encryption key: %w",
146146
err)
147147
}
148148

@@ -151,11 +151,11 @@ func decryptMacaroon(keyB64, dataB64 string, pw []byte) ([]byte, error) {
151151
err = key.DeriveKey(&pw)
152152
if err != nil {
153153
return nil, fmt.Errorf("could not derive encryption key, "+
154-
"possibly due to incorrect password: %v", err)
154+
"possibly due to incorrect password: %w", err)
155155
}
156156
macBytes, err := key.Decrypt(encryptedMac)
157157
if err != nil {
158-
return nil, fmt.Errorf("could not decrypt macaroon data: %v",
158+
return nil, fmt.Errorf("could not decrypt macaroon data: %w",
159159
err)
160160
}
161161
return macBytes, nil

cmd/tapcli/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
9090
// created from the global options in the CLI context.
9191
profile, err := getGlobalOptions(ctx, skipMacaroons)
9292
if err != nil {
93-
fatal(fmt.Errorf("could not load global options: %v", err))
93+
fatal(fmt.Errorf("could not load global options: %w", err))
9494
}
9595

9696
// Load the specified TLS certificate.
9797
certPool, err := profile.cert()
9898
if err != nil {
99-
fatal(fmt.Errorf("could not create cert pool: %v", err))
99+
fatal(fmt.Errorf("could not create cert pool: %w", err))
100100
}
101101

102102
// Build transport credentials from the certificate pool. If there is no
@@ -143,7 +143,7 @@ func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
143143
// don't need to ask for it every time.
144144
mac, err := macEntry.loadMacaroon(readPassword)
145145
if err != nil {
146-
fatal(fmt.Errorf("could not load macaroon: %v", err))
146+
fatal(fmt.Errorf("could not load macaroon: %w", err))
147147
}
148148

149149
macConstraints := []macaroons.Constraint{
@@ -174,7 +174,7 @@ func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
174174
// Now we append the macaroon credentials to the dial options.
175175
cred, err := macaroons.NewMacaroonCredential(constrainedMac)
176176
if err != nil {
177-
fatal(fmt.Errorf("error cloning mac: %v", err))
177+
fatal(fmt.Errorf("error cloning mac: %w", err))
178178
}
179179
opts = append(opts, grpc.WithPerRPCCredentials(cred))
180180
}
@@ -201,7 +201,7 @@ func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
201201

202202
conn, err := grpc.Dial(profile.RPCServer, opts...)
203203
if err != nil {
204-
fatal(fmt.Errorf("unable to connect to RPC server: %v", err))
204+
fatal(fmt.Errorf("unable to connect to RPC server: %w", err))
205205
}
206206

207207
return conn

cmd/tapcli/profile.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) (
170170
var err error
171171
tlsCert, err = os.ReadFile(tlsCertPath)
172172
if err != nil {
173-
return nil, fmt.Errorf("could not load TLS cert file: %v", err)
173+
return nil, fmt.Errorf("could not load TLS cert file: %w", err)
174174
}
175175
}
176176

@@ -194,11 +194,11 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) (
194194
macBytes, err := os.ReadFile(macPath)
195195
if err != nil {
196196
return nil, fmt.Errorf("unable to read macaroon path (check "+
197-
"the network setting!): %v", err)
197+
"the network setting!): %w", err)
198198
}
199199
mac := &macaroon.Macaroon{}
200200
if err = mac.UnmarshalBinary(macBytes); err != nil {
201-
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
201+
return nil, fmt.Errorf("unable to decode macaroon: %w", err)
202202
}
203203

204204
var pw []byte
@@ -212,12 +212,12 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) (
212212
)
213213
if err != nil {
214214
return nil, fmt.Errorf("unable to get encryption "+
215-
"password: %v", err)
215+
"password: %w", err)
216216
}
217217
}
218218
macEntry := &macaroonEntry{}
219219
if err = macEntry.storeMacaroon(mac, pw); err != nil {
220-
return nil, fmt.Errorf("unable to store macaroon: %v", err)
220+
return nil, fmt.Errorf("unable to store macaroon: %w", err)
221221
}
222222

223223
// We determine the name of the macaroon from the file itself but cut
@@ -248,7 +248,7 @@ func loadProfileFile(file string) (*profileFile, error) {
248248

249249
content, err := os.ReadFile(file)
250250
if err != nil {
251-
return nil, fmt.Errorf("could not load profile file %s: %v",
251+
return nil, fmt.Errorf("could not load profile file %s: %w",
252252
file, err)
253253
}
254254
f := &profileFile{}
@@ -265,7 +265,7 @@ func loadProfileFile(file string) (*profileFile, error) {
265265
func saveProfileFile(file string, f *profileFile) error {
266266
content, err := f.marshalJSON()
267267
if err != nil {
268-
return fmt.Errorf("could not marshal profile: %v", err)
268+
return fmt.Errorf("could not marshal profile: %w", err)
269269
}
270270
return os.WriteFile(file, content, 0644)
271271
}
@@ -286,14 +286,14 @@ func (f *profileFile) unmarshalJSON(content []byte) error {
286286
func (f *profileFile) marshalJSON() ([]byte, error) {
287287
b, err := json.Marshal(f)
288288
if err != nil {
289-
return nil, fmt.Errorf("error JSON marshalling profile: %v",
289+
return nil, fmt.Errorf("error JSON marshalling profile: %w",
290290
err)
291291
}
292292

293293
var out bytes.Buffer
294294
err = json.Indent(&out, b, "", " ")
295295
if err != nil {
296-
return nil, fmt.Errorf("error indenting profile JSON: %v", err)
296+
return nil, fmt.Errorf("error indenting profile JSON: %w", err)
297297
}
298298
out.WriteString("\n")
299299
return out.Bytes(), nil

cmd/tapd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func main() {
7070
cfg, cfgLogger, shutdownInterceptor, errQueue.ChanIn(),
7171
)
7272
if err != nil {
73-
err := fmt.Errorf("error creating server: %v", err)
73+
err := fmt.Errorf("error creating server: %w", err)
7474
_, _ = fmt.Fprintln(os.Stderr, err)
7575
os.Exit(1)
7676
}

fn/errors_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ func TestIsCanceled(t *testing.T) {
1414
require.True(t, IsCanceled(context.Canceled))
1515
require.True(t, IsCanceled(errRpcCanceled))
1616
require.True(t, IsCanceled(fmt.Errorf("foo: %w", context.Canceled)))
17-
require.True(t, IsCanceled(fmt.Errorf("foo: %v", errRpcCanceled)))
17+
require.True(t, IsCanceled(fmt.Errorf("foo: %w", errRpcCanceled)))
1818
}

itest/assertions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func AssetAnchorCheck(txid, blockHash chainhash.Hash) AssetCheck {
8686
out, err :=
8787
wire.NewOutPointFromString(a.ChainAnchor.AnchorOutpoint)
8888
if err != nil {
89-
return fmt.Errorf("unable to parse outpoint: %v", err)
89+
return fmt.Errorf("unable to parse outpoint: %w", err)
9090
}
9191

9292
anchorTxid := out.Hash.String()

0 commit comments

Comments
 (0)