Skip to content

Commit

Permalink
Merge pull request lightninglabs#812 from lightninglabs/lint-err-wrap
Browse files Browse the repository at this point in the history
Linter checks for `fmt.Errorf` error wrap misuse
  • Loading branch information
ffranr authored Feb 23, 2024
2 parents d37b486 + f00697f commit f756868
Show file tree
Hide file tree
Showing 33 changed files with 111 additions and 105 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ run:
deadline: 4m

linters-settings:
errorlint:
comparison: false
asserts: false
govet:
# Don't report about shadowed variables
check-shadowing: false
Expand All @@ -26,6 +29,7 @@ linters-settings:

linters:
enable:
- errorlint
- gofmt
- tagliatelle
- whitespace
Expand Down
4 changes: 2 additions & 2 deletions cmd/tapcli/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func newAddr(ctx *cli.Context) error {

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

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

printRespJSON(addrs)
Expand Down
20 changes: 10 additions & 10 deletions cmd/tapcli/cmd_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func profileAdd(ctx *cli.Context) error {
// Create a profile struct from all the global options.
profile, err := profileFromContext(ctx, true, false)
if err != nil {
return fmt.Errorf("could not load global options: %v", err)
return fmt.Errorf("could not load global options: %w", err)
}

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

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

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

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

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

// Finally, all that's left is to get the profile name from either
Expand Down Expand Up @@ -420,25 +420,25 @@ func profileAddMacaroon(ctx *cli.Context) error {
macPath := lncfg.CleanAndExpandPath(ctx.GlobalString("macaroonpath"))
macBytes, err := os.ReadFile(macPath)
if err != nil {
return fmt.Errorf("unable to read macaroon path: %v", err)
return fmt.Errorf("unable to read macaroon path: %w", err)
}
mac := &macaroon.Macaroon{}
if err = mac.UnmarshalBinary(macBytes); err != nil {
return fmt.Errorf("unable to decode macaroon: %v", err)
return fmt.Errorf("unable to decode macaroon: %w", err)
}
macEntry := &macaroonEntry{
Name: macName,
}
if err = macEntry.storeMacaroon(mac, nil); err != nil {
return fmt.Errorf("unable to store macaroon: %v", err)
return fmt.Errorf("unable to store macaroon: %w", err)
}

// All done, store the updated profile file.
selectedProfile.Macaroons.Jar = append(
selectedProfile.Macaroons.Jar, macEntry,
)
if err = saveProfileFile(defaultProfileFile, f); err != nil {
return fmt.Errorf("error writing profile file %s: %v",
return fmt.Errorf("error writing profile file %s: %w",
defaultProfileFile, err)
}

Expand Down
24 changes: 12 additions & 12 deletions cmd/tapcli/macaroon_jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,26 @@ func (e *macaroonEntry) loadMacaroon(
pw, err := pwCallback("Enter macaroon encryption password: ")
if err != nil {
return nil, fmt.Errorf("could not read password from "+
"terminal: %v", err)
"terminal: %w", err)
}

macBytes, err = decryptMacaroon(parts[1], parts[2], pw)
if err != nil {
return nil, fmt.Errorf("unable to decrypt macaroon: %v",
return nil, fmt.Errorf("unable to decrypt macaroon: %w",
err)
}
} else {
macBytes, err = hex.DecodeString(e.Data)
if err != nil {
return nil, fmt.Errorf("unable to hex decode "+
"macaroon: %v", err)
"macaroon: %w", err)
}
}

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

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

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

keyB64 := base64.StdEncoding.EncodeToString(key.Marshal())
Expand All @@ -130,19 +130,19 @@ func decryptMacaroon(keyB64, dataB64 string, pw []byte) ([]byte, error) {
keyData, err := base64.StdEncoding.DecodeString(keyB64)
if err != nil {
return nil, fmt.Errorf("could not base64 decode encryption "+
"key: %v", err)
"key: %w", err)
}
encryptedMac, err := base64.StdEncoding.DecodeString(dataB64)
if err != nil {
return nil, fmt.Errorf("could not base64 decode macaroon "+
"data: %v", err)
"data: %w", err)
}

// Unmarshal the encryption key and ask the user for the password.
key := &snacl.SecretKey{}
err = key.Unmarshal(keyData)
if err != nil {
return nil, fmt.Errorf("could not unmarshal encryption key: %v",
return nil, fmt.Errorf("could not unmarshal encryption key: %w",
err)
}

Expand All @@ -151,11 +151,11 @@ func decryptMacaroon(keyB64, dataB64 string, pw []byte) ([]byte, error) {
err = key.DeriveKey(&pw)
if err != nil {
return nil, fmt.Errorf("could not derive encryption key, "+
"possibly due to incorrect password: %v", err)
"possibly due to incorrect password: %w", err)
}
macBytes, err := key.Decrypt(encryptedMac)
if err != nil {
return nil, fmt.Errorf("could not decrypt macaroon data: %v",
return nil, fmt.Errorf("could not decrypt macaroon data: %w",
err)
}
return macBytes, nil
Expand Down
10 changes: 5 additions & 5 deletions cmd/tapcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
// created from the global options in the CLI context.
profile, err := getGlobalOptions(ctx, skipMacaroons)
if err != nil {
fatal(fmt.Errorf("could not load global options: %v", err))
fatal(fmt.Errorf("could not load global options: %w", err))
}

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

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

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

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

return conn
Expand Down
18 changes: 9 additions & 9 deletions cmd/tapcli/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) (
var err error
tlsCert, err = os.ReadFile(tlsCertPath)
if err != nil {
return nil, fmt.Errorf("could not load TLS cert file: %v", err)
return nil, fmt.Errorf("could not load TLS cert file: %w", err)
}
}

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

var pw []byte
Expand All @@ -212,12 +212,12 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) (
)
if err != nil {
return nil, fmt.Errorf("unable to get encryption "+
"password: %v", err)
"password: %w", err)
}
}
macEntry := &macaroonEntry{}
if err = macEntry.storeMacaroon(mac, pw); err != nil {
return nil, fmt.Errorf("unable to store macaroon: %v", err)
return nil, fmt.Errorf("unable to store macaroon: %w", err)
}

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

content, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("could not load profile file %s: %v",
return nil, fmt.Errorf("could not load profile file %s: %w",
file, err)
}
f := &profileFile{}
Expand All @@ -265,7 +265,7 @@ func loadProfileFile(file string) (*profileFile, error) {
func saveProfileFile(file string, f *profileFile) error {
content, err := f.marshalJSON()
if err != nil {
return fmt.Errorf("could not marshal profile: %v", err)
return fmt.Errorf("could not marshal profile: %w", err)
}
return os.WriteFile(file, content, 0644)
}
Expand All @@ -286,14 +286,14 @@ func (f *profileFile) unmarshalJSON(content []byte) error {
func (f *profileFile) marshalJSON() ([]byte, error) {
b, err := json.Marshal(f)
if err != nil {
return nil, fmt.Errorf("error JSON marshalling profile: %v",
return nil, fmt.Errorf("error JSON marshalling profile: %w",
err)
}

var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
if err != nil {
return nil, fmt.Errorf("error indenting profile JSON: %v", err)
return nil, fmt.Errorf("error indenting profile JSON: %w", err)
}
out.WriteString("\n")
return out.Bytes(), nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/tapd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
cfg, cfgLogger, shutdownInterceptor, errQueue.ChanIn(),
)
if err != nil {
err := fmt.Errorf("error creating server: %v", err)
err := fmt.Errorf("error creating server: %w", err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion fn/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func TestIsCanceled(t *testing.T) {
require.True(t, IsCanceled(context.Canceled))
require.True(t, IsCanceled(errRpcCanceled))
require.True(t, IsCanceled(fmt.Errorf("foo: %w", context.Canceled)))
require.True(t, IsCanceled(fmt.Errorf("foo: %v", errRpcCanceled)))
require.True(t, IsCanceled(fmt.Errorf("foo: %w", errRpcCanceled)))
}
2 changes: 1 addition & 1 deletion itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func AssetAnchorCheck(txid, blockHash chainhash.Hash) AssetCheck {
out, err :=
wire.NewOutPointFromString(a.ChainAnchor.AnchorOutpoint)
if err != nil {
return fmt.Errorf("unable to parse outpoint: %v", err)
return fmt.Errorf("unable to parse outpoint: %w", err)
}

anchorTxid := out.Hash.String()
Expand Down
Loading

0 comments on commit f756868

Please sign in to comment.