Skip to content

Commit ed10f97

Browse files
committed
fix: fmt.Errorf formats
Signed-off-by: Brian McGee <[email protected]>
1 parent c720e41 commit ed10f97

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

cache/cache.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,38 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)
5555
name := hex.EncodeToString(digest)
5656
path, err := xdg.CacheFile(fmt.Sprintf("treefmt/eval-cache/%v.db", name))
5757
if err != nil {
58-
return fmt.Errorf("%w: could not resolve local path for the cache", err)
58+
return fmt.Errorf("could not resolve local path for the cache: %w", err)
5959
}
6060

6161
db, err = bolt.Open(path, 0o600, nil)
6262
if err != nil {
63-
return fmt.Errorf("%w: failed to open cache", err)
63+
return fmt.Errorf("failed to open cache at %v: %w", path, err)
6464
}
6565

6666
err = db.Update(func(tx *bolt.Tx) error {
6767
// create bucket for tracking paths
6868
pathsBucket, err := tx.CreateBucketIfNotExists([]byte(pathsBucket))
6969
if err != nil {
70-
return fmt.Errorf("%w: failed to create paths bucket", err)
70+
return fmt.Errorf("failed to create paths bucket: %w", err)
7171
}
7272

7373
// create bucket for tracking formatters
7474
formattersBucket, err := tx.CreateBucketIfNotExists([]byte(formattersBucket))
7575
if err != nil {
76-
return fmt.Errorf("%w: failed to create formatters bucket", err)
76+
return fmt.Errorf("failed to create formatters bucket: %w", err)
7777
}
7878

7979
// check for any newly configured or modified formatters
8080
for name, formatter := range formatters {
8181

8282
stat, err := os.Lstat(formatter.Executable())
8383
if err != nil {
84-
return fmt.Errorf("%w: failed to state formatter executable", err)
84+
return fmt.Errorf("failed to stat formatter executable %v: %w", formatter.Executable(), err)
8585
}
8686

8787
entry, err := getEntry(formattersBucket, name)
8888
if err != nil {
89-
return fmt.Errorf("%w: failed to retrieve entry for formatter", err)
89+
return fmt.Errorf("failed to retrieve cache entry for formatter %v: %w", name, err)
9090
}
9191

9292
clean = clean || entry == nil || !(entry.Size == stat.Size() && entry.Modified == stat.ModTime())
@@ -105,7 +105,7 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)
105105
}
106106

107107
if err = putEntry(formattersBucket, name, entry); err != nil {
108-
return fmt.Errorf("%w: failed to write formatter entry", err)
108+
return fmt.Errorf("failed to write cache entry for formatter %v: %w", name, err)
109109
}
110110
}
111111

@@ -115,22 +115,22 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)
115115
if !ok {
116116
// remove the formatter entry from the cache
117117
if err = formattersBucket.Delete(key); err != nil {
118-
return fmt.Errorf("%w: failed to remove formatter entry", err)
118+
return fmt.Errorf("failed to remove cache entry for formatter %v: %w", key, err)
119119
}
120120
// indicate a clean is required
121121
clean = true
122122
}
123123
return nil
124124
}); err != nil {
125-
return fmt.Errorf("%w: failed to check for removed formatters", err)
125+
return fmt.Errorf("failed to check cache for removed formatters: %w", err)
126126
}
127127

128128
if clean {
129129
// remove all path entries
130130
c := pathsBucket.Cursor()
131131
for k, v := c.First(); !(k == nil && v == nil); k, v = c.Next() {
132132
if err = c.Delete(); err != nil {
133-
return fmt.Errorf("%w: failed to remove path entry", err)
133+
return fmt.Errorf("failed to remove path entry: %w", err)
134134
}
135135
}
136136
}
@@ -155,7 +155,7 @@ func getEntry(bucket *bolt.Bucket, path string) (*Entry, error) {
155155
if b != nil {
156156
var cached Entry
157157
if err := msgpack.Unmarshal(b, &cached); err != nil {
158-
return nil, fmt.Errorf("%w: failed to unmarshal cache info for path '%v'", err, path)
158+
return nil, fmt.Errorf("failed to unmarshal cache info for path '%v': %w", path, err)
159159
}
160160
return &cached, nil
161161
} else {
@@ -167,11 +167,11 @@ func getEntry(bucket *bolt.Bucket, path string) (*Entry, error) {
167167
func putEntry(bucket *bolt.Bucket, path string, entry *Entry) error {
168168
bytes, err := msgpack.Marshal(entry)
169169
if err != nil {
170-
return fmt.Errorf("%w: failed to marshal cache entry", err)
170+
return fmt.Errorf("failed to marshal cache path %v: %w", path, err)
171171
}
172172

173173
if err = bucket.Put([]byte(path), bytes); err != nil {
174-
return fmt.Errorf("%w: failed to put cache entry", err)
174+
return fmt.Errorf("failed to put cache path %v: %w", path, err)
175175
}
176176
return nil
177177
}
@@ -202,7 +202,7 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil
202202
return ctx.Err()
203203
default:
204204
if err != nil {
205-
return fmt.Errorf("%w: failed to walk path", err)
205+
return fmt.Errorf("failed to walk path: %w", err)
206206
} else if file.Info.IsDir() {
207207
// ignore directories
208208
return nil
@@ -219,7 +219,7 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil
219219
if tx == nil {
220220
tx, err = db.Begin(false)
221221
if err != nil {
222-
return fmt.Errorf("%w: failed to open a new read tx", err)
222+
return fmt.Errorf("failed to open a new cache read tx: %w", err)
223223
}
224224
bucket = tx.Bucket([]byte(pathsBucket))
225225
}

cli/format.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ func (f *Format) Run() (err error) {
5252
// read config
5353
cfg, err := config.ReadFile(Cli.ConfigFile, Cli.Formatters)
5454
if err != nil {
55-
return fmt.Errorf("%w: failed to read config file", err)
55+
return fmt.Errorf("failed to read config file %v: %w", Cli.ConfigFile, err)
5656
}
5757

5858
// compile global exclude globs
5959
if globalExcludes, err = format.CompileGlobs(cfg.Global.Excludes); err != nil {
60-
return fmt.Errorf("%w: failed to compile global globs", err)
60+
return fmt.Errorf("failed to compile global excludes: %w", err)
6161
}
6262

6363
// initialise pipelines

cli/helpers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ func cmd(t *testing.T, args ...string) ([]byte, error) {
5757

5858
// reset and read the temporary output
5959
if _, err = tempOut.Seek(0, 0); err != nil {
60-
return nil, fmt.Errorf("%w: failed to reset temp output for reading", err)
60+
return nil, fmt.Errorf("failed to reset temp output for reading: %w", err)
6161
}
6262

6363
out, err := io.ReadAll(tempOut)
6464
if err != nil {
65-
return nil, fmt.Errorf("%w: failed to read temp output", err)
65+
return nil, fmt.Errorf("failed to read temp output: %w", err)
6666
}
6767

6868
// swap outputs back

format/formatter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (f *Formatter) Apply(ctx context.Context, files []*walk.File, filter bool)
8787
if len(out) > 0 {
8888
_, _ = fmt.Fprintf(os.Stderr, "%s error:\n%s\n", f.name, out)
8989
}
90-
return fmt.Errorf("%w: formatter %s failed to apply", err, f.name)
90+
return fmt.Errorf("formatter %s failed to apply: %w", f.name, err)
9191
}
9292

9393
//
@@ -141,12 +141,12 @@ func NewFormatter(
141141

142142
f.includes, err = CompileGlobs(cfg.Includes)
143143
if err != nil {
144-
return nil, fmt.Errorf("%w: formatter '%v' includes", err, f.name)
144+
return nil, fmt.Errorf("failed to compile formatter '%v' includes: %w", f.name, err)
145145
}
146146

147147
f.excludes, err = CompileGlobs(cfg.Excludes)
148148
if err != nil {
149-
return nil, fmt.Errorf("%w: formatter '%v' excludes", err, f.name)
149+
return nil, fmt.Errorf("failed to compile formatter '%v' excludes: %w", f.name, err)
150150
}
151151
f.excludes = append(f.excludes, globalExcludes...)
152152

format/glob.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func CompileGlobs(patterns []string) ([]glob.Glob, error) {
1313
for i, pattern := range patterns {
1414
g, err := glob.Compile(pattern)
1515
if err != nil {
16-
return nil, fmt.Errorf("%w: failed to compile include pattern '%v'", err, pattern)
16+
return nil, fmt.Errorf("failed to compile include pattern '%v': %w", pattern, err)
1717
}
1818
globs[i] = g
1919
}

walk/git.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
3737

3838
idx, err := g.repo.Storer.Index()
3939
if err != nil {
40-
return fmt.Errorf("%w: failed to open index", err)
40+
return fmt.Errorf("failed to open git index: %w", err)
4141
}
4242

4343
if len(g.paths) > 0 {
@@ -102,7 +102,7 @@ func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
102102
func NewGit(root string, paths []string) (Walker, error) {
103103
repo, err := git.PlainOpen(root)
104104
if err != nil {
105-
return nil, fmt.Errorf("%w: failed to open git repo", err)
105+
return nil, fmt.Errorf("failed to open git repo: %w", err)
106106
}
107107
return &gitWalker{root, paths, repo}, nil
108108
}

0 commit comments

Comments
 (0)