Skip to content

Commit

Permalink
adapt code to errorlint suggestion
Browse files Browse the repository at this point in the history
Adapt the code to the errorlint suggest.
Using errors.Is/As is often unnecessary in the current code but it will make the
code more resilient if code is changed to wrap errors instead.
  • Loading branch information
fho committed Jan 9, 2024
1 parent 9933a74 commit 0e9338f
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 27 deletions.
4 changes: 2 additions & 2 deletions internal/command/diff_inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func getPreviousTaskRun(psql storage.Storer, argDetails *diffInputArgDetails) *s
},
)

if err != nil && errors.Unwrap(err) != found {
if err != nil && errors.Unwrap(err) != found { //nolint:errorlint
exitOnErr(err)
}

Expand Down Expand Up @@ -331,7 +331,7 @@ func getTaskRunByID(psql storage.Storer, id int) *storage.TaskRunWithID {
},
)

if err != nil && err == storage.ErrNotExist {
if errors.Is(err, storage.ErrNotExist) {
err = fmt.Errorf("task-run %d does not exist", id)
}
exitOnErr(err)
Expand Down
5 changes: 3 additions & 2 deletions internal/command/ls_outputs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"errors"
"os"
"strconv"

Expand Down Expand Up @@ -59,15 +60,15 @@ func (c *lsOutputsCmd) run(_ *cobra.Command, args []string) {

_, err = pgClient.TaskRun(ctx, taskRunID)
if err != nil {
if err == storage.ErrNotExist {
if errors.Is(err, storage.ErrNotExist) {
stderr.Printf("task run with ID %d does not exist", taskRunID)
exitFunc(1)
}
}

outputs, err := pgClient.Outputs(ctx, taskRunID)
if err != nil {
if err == storage.ErrNotExist {
if errors.Is(err, storage.ErrNotExist) {
log.Debugf("task run with ID %d has no outputs", taskRunID)
} else {
exitOnErr(err)
Expand Down
3 changes: 2 additions & 1 deletion internal/command/ls_runs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -168,7 +169,7 @@ func (c *lsRunsCmd) run(_ *cobra.Command, args []string) {
)

if err != nil {
if err == storage.ErrNotExist {
if errors.Is(err, storage.ErrNotExist) {
stderr.Println("no matching task runs exist")
exitFunc(1)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (*showCmd) showBuild(taskRunID int) {

taskRun, err := storageClt.TaskRun(ctx, taskRunID)
if err != nil {
if err == storage.ErrNotExist {
if errors.Is(err, storage.ErrNotExist) {
stderr.Printf("task run with id %d does not exist\n", taskRunID)
exitFunc(1)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/exec/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ func (c *Cmd) logf(format string, a ...any) {

func exitCodeFromErr(waitErr error) (exitCode int, err error) {
var ee *exec.ExitError
var ok bool

if waitErr == nil {
return 0, err
}

if ee, ok = waitErr.(*exec.ExitError); !ok {
if !errors.As(waitErr, &ee) {
return -1, waitErr
}

Expand Down
3 changes: 2 additions & 1 deletion internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fs

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -174,7 +175,7 @@ func FileReadLine(path string) (string, error) {

r := bufio.NewReader(fd)
content, err := r.ReadString('\n')
if err != nil && err != io.EOF {
if err != nil && !errors.Is(err, io.EOF) {
return "", err
}

Expand Down
3 changes: 2 additions & 1 deletion internal/testutils/logwriter/logwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logwriter

import (
"bytes"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (l *Logger) Write(p []byte) (int, error) {
for {
line, err := l.buf.ReadString('\n')
if err != nil {
if err != io.EOF {
if !errors.Is(err, io.EOF) {
panic(fmt.Sprintf("logwrap: reading from buffer failed: %s", err))
}
// add chunk without line-ending back to buffer
Expand Down
11 changes: 7 additions & 4 deletions internal/upload/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package docker
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/url"
Expand Down Expand Up @@ -163,7 +164,7 @@ func (c *Client) Upload(image, registryAddr, repository, tag string) (string, er
for {
outStream.Flush()
line, err := outBuf.ReadString('\n')
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}

Expand All @@ -190,9 +191,11 @@ func (c *Client) SizeBytes(imageID string) (int64, error) {
// Exists return true if the image with the given ID exist, otherwise false.
func (c *Client) Exists(imageID string) (bool, error) {
_, err := c.clt.InspectImage(imageID)
if err != nil && err != docker.ErrNoSuchImage {
if err != nil {
if errors.Is(err, docker.ErrNoSuchImage) {
return false, nil
}
return false, err
}

return err != docker.ErrNoSuchImage, nil
return true, nil
}
3 changes: 2 additions & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version

import (
_ "embed" // is required to initialize the rawVersion variable with content from the ver file
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -77,7 +78,7 @@ func New(ver string) (*SemVer, error) {

ver = strings.TrimSpace(ver)
matches, err := fmt.Sscanf(ver, "%d.%d.%d-%s", &major, &minor, &patch, &appendix)
if (err != nil && err != io.ErrUnexpectedEOF) || matches < 1 {
if (err != nil && !errors.Is(err, io.ErrUnexpectedEOF)) || matches < 1 {
return nil, fmt.Errorf("invalid format, should be <Major>[.<Minor>[.<Patch>[-appendix]]]: %w", err)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/baur/cfgupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (u *CfgUpgrader) upgradeAppConfigs(

if err := appCfg.Validate(); err != nil {
if appCfg.Name != "" {
return fmt.Errorf("%s: %s", appCfg.Name, err)
return fmt.Errorf("%s: %w", appCfg.Name, err)
}

return fmt.Errorf("%s: %s", cfgPath, err)
return fmt.Errorf("%s: %w", cfgPath, err)
}

newAppCfg := v4.UpgradeAppConfig(appCfg)
Expand Down Expand Up @@ -142,7 +142,7 @@ func (u *CfgUpgrader) upgradeV4() error {
}

if err := oldInclude.Validate(); err != nil {
return fmt.Errorf("%s: %s", includePath, err)
return fmt.Errorf("%s: %w", includePath, err)
}

newInclude := v4.UpgradeIncludeConfig(oldInclude)
Expand Down
3 changes: 2 additions & 1 deletion pkg/baur/taskstatusevaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package baur

import (
"context"
"errors"
"fmt"

"github.com/simplesurance/baur/v3/pkg/storage"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (t *TaskStatusEvaluator) getTaskStatus(ctx context.Context, inputs *Inputs,

run, err := t.store.LatestTaskRunByDigest(ctx, task.AppName, task.Name, totalInputDigest.String())
if err != nil {
if err == storage.ErrNotExist {
if errors.Is(err, storage.ErrNotExist) {
return TaskStatusExecutionPending, nil, nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cfg/fielderror.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func newFieldError(msg string, path ...string) *fieldError {
// If it is of type FieldError, the passed paths are prepended to it's
// ElementPath and err is returned.
func fieldErrorWrap(err error, path ...string) error {
valError, ok := err.(*fieldError)
if ok {
valError.elementPath = append(path, valError.elementPath...)
var fErr *fieldError
if errors.As(err, &fErr) {
fErr.elementPath = append(path, fErr.elementPath...)
return err
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/cfg/taskdef.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cfg

import (
"errors"
"fmt"
"strings"
)
Expand Down Expand Up @@ -29,13 +30,13 @@ func taskMerge(task taskDef, workingDir string, resolver Resolver, includeDB *In
// The includeSpec can refer to an input or output.
// If no input include for it exist, ErrIncludeIDNotFound is
// ignored and we try to load an output include instead.
if err != nil && err != ErrIncludeIDNotFound {
if err != nil && !errors.Is(err, ErrIncludeIDNotFound) {
return fieldErrorWrap(fmt.Errorf("%q: %w", includeSpec, err), "Includes")
}

outputInclude, err := includeDB.loadOutputInclude(resolver, workingDir, includeSpec)
if err != nil {
if err == ErrIncludeIDNotFound {
if errors.Is(err, ErrIncludeIDNotFound) {
return fieldErrorWrap(fmt.Errorf("%q: %w", includeSpec, err), "Includes")
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/postgres/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *Client) inputStrings(ctx context.Context, taskRunID int) ([]*storage.In

rows, err := c.db.Query(ctx, query, taskRunID)
if err != nil {
if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
return nil, storage.ErrNotExist
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func (c *Client) inputFiles(ctx context.Context, taskRunID int) ([]*storage.Inpu

rows, err := c.db.Query(ctx, query, taskRunID)
if err != nil {
if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
return nil, storage.ErrNotExist
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func (c *Client) inputEnvVars(ctx context.Context, taskRunID int) ([]*storage.In

rows, err := c.db.Query(ctx, query, taskRunID)
if err != nil {
if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
return nil, storage.ErrNotExist
}

Expand Down

0 comments on commit 0e9338f

Please sign in to comment.