Skip to content

Commit 3ca260c

Browse files
committed
Enable errorlint linter and fix 18 issues
Fix three categories of error handling issues: - Use errors.Is instead of == for error comparison (6 fixes) io.EOF, sql.ErrNoRows, http.ErrServerClosed, context.Canceled - Use errors.As instead of type assertion on errors (4 fixes) *exec.ExitError, *environment.RequiredEnvError, cli.RuntimeError - Use %w instead of %v in fmt.Errorf for proper wrapping (8 fixes) api.go, eval.go, store.go, sqliteutil.go Assisted-By: cagent
1 parent d296164 commit 3ca260c

16 files changed

Lines changed: 34 additions & 18 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ linters:
1414
- durationcheck
1515
- errcheck
1616
- errname
17+
- errorlint
1718
- exptostd
1819
- fatcontext
1920
- forbidigo

cmd/root/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package root
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"log/slog"
@@ -422,7 +423,8 @@ func (f *runExecFlags) handleExecMode(ctx context.Context, out *cli.Printer, rt
422423
OutputJSON: f.outputJSON,
423424
AutoApprove: f.autoApprove,
424425
}, rt, sess, execArgs)
425-
if cliErr, ok := err.(cli.RuntimeError); ok {
426+
var cliErr cli.RuntimeError
427+
if errors.As(err, &cliErr) {
426428
return RuntimeError{Err: cliErr.Err}
427429
}
428430
return err

pkg/config/config_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ func TestCheckRequiredEnvVars(t *testing.T) {
271271
require.NoError(t, err)
272272
} else {
273273
require.Error(t, err)
274-
assert.Equal(t, test.expectedMissing, err.(*environment.RequiredEnvError).Missing)
274+
var reqErr *environment.RequiredEnvError
275+
require.ErrorAs(t, err, &reqErr)
276+
assert.Equal(t, test.expectedMissing, reqErr.Missing)
275277
}
276278
})
277279
}

pkg/evaluation/eval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (r *Runner) preBuildImages(ctx context.Context, out io.Writer, evals []Inpu
272272
}
273273

274274
if len(errs) > 0 {
275-
return fmt.Errorf("failed to build %d image(s): %v", len(errs), errs[0])
275+
return fmt.Errorf("failed to build %d image(s): %w", len(errs), errs[0])
276276
}
277277

278278
return nil

pkg/fake/proxy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bufio"
77
"bytes"
88
"context"
9+
"errors"
910
"fmt"
1011
"io"
1112
"log/slog"
@@ -428,7 +429,7 @@ func StreamCopy(c echo.Context, resp *http.Response) error {
428429
}
429430
if result.err != nil {
430431
// io.EOF or context canceled means normal completion
431-
if result.err == io.EOF || ctx.Err() != nil {
432+
if errors.Is(result.err, io.EOF) || ctx.Err() != nil {
432433
return nil //nolint:nilerr // EOF and context cancellation are normal stream termination
433434
}
434435
slog.ErrorContext(ctx, "stream read error", "error", result.err)

pkg/fsx/fs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fsx
22

33
import (
44
"context"
5+
"errors"
56
"io/fs"
67
"os"
78
"path/filepath"
@@ -209,7 +210,7 @@ func WalkFiles(ctx context.Context, root string, opts WalkFilesOptions) ([]strin
209210
return nil
210211
})
211212

212-
if err != nil && err != context.Canceled && err != context.DeadlineExceeded {
213+
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
213214
return files, err
214215
}
215216

pkg/hooks/executor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"cmp"
66
"context"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"log/slog"
1011
"os"
@@ -264,7 +265,8 @@ func (e *Executor) executeHook(ctx context.Context, hook Hook, inputJSON []byte)
264265

265266
exitCode := 0
266267
if err != nil {
267-
if exitErr, ok := err.(*exec.ExitError); ok {
268+
var exitErr *exec.ExitError
269+
if errors.As(err, &exitErr) {
268270
exitCode = exitErr.ExitCode()
269271
} else {
270272
return nil, stdout.String(), stderr.String(), -1, err

pkg/mcp/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mcp
33
import (
44
"cmp"
55
"context"
6+
"errors"
67
"fmt"
78
"log/slog"
89
"net"
@@ -74,7 +75,7 @@ func StartHTTPServer(ctx context.Context, agentFilename, agentName string, runCo
7475
case <-ctx.Done():
7576
return httpServer.Shutdown(context.Background())
7677
case err := <-errCh:
77-
if err == http.ErrServerClosed {
78+
if errors.Is(err, http.ErrServerClosed) {
7879
return nil
7980
}
8081
return err

pkg/model/provider/custom_provider_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"io"
78
"net/http"
89
"net/http/httptest"
@@ -502,7 +503,7 @@ func drainStream(t *testing.T, stream chat.MessageStream) {
502503
t.Helper()
503504
for {
504505
_, err := stream.Recv()
505-
if err == io.EOF {
506+
if errors.Is(err, io.EOF) {
506507
return
507508
}
508509
if err != nil {

pkg/rag/strategy/bm25_database.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package strategy
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"log/slog"
89
"os"
@@ -96,7 +97,7 @@ func (d *bm25DB) AddDocument(ctx context.Context, doc database.Document) error {
9697
fmt.Sprintf("SELECT 1 FROM %s WHERE source_path = ? AND chunk_index = ?", d.docsTable),
9798
doc.SourcePath, doc.ChunkIndex).Scan(&exists)
9899

99-
if err != nil && err != sql.ErrNoRows {
100+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
100101
return fmt.Errorf("failed to check existing document: %w", err)
101102
}
102103

0 commit comments

Comments
 (0)