Skip to content

Commit 2ef22f8

Browse files
authored
Merge pull request #4745 from kersten/chore/wrap-plugin-utils-errors
🌱 (chore): wrap plugin and util error returns for better context
2 parents 96a5d65 + eb284dc commit 2ef22f8

File tree

3 files changed

+67
-45
lines changed

3 files changed

+67
-45
lines changed

pkg/plugin/util/exec.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"os/exec"
2223
"strings"
@@ -30,5 +31,10 @@ func RunCmd(msg, cmd string, args ...string) error {
3031
c.Stdout = os.Stdout
3132
c.Stderr = os.Stderr
3233
log.Println(msg + ":\n$ " + strings.Join(c.Args, " "))
33-
return c.Run()
34+
35+
if err := c.Run(); err != nil {
36+
return fmt.Errorf("error running %q: %w", cmd, err)
37+
}
38+
39+
return nil
3440
}

pkg/plugin/util/util.go

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func RandomSuffix() (string, error) {
4141
bi := new(big.Int)
4242
r, err := rand.Int(rand.Reader, bi.SetInt64(int64(len(source))))
4343
if err != nil {
44-
return "", err
44+
return "", fmt.Errorf("failed to generate random number: %w", err)
4545
}
4646
res[i] = source[r.Int64()]
4747
}
@@ -67,23 +67,27 @@ func InsertCode(filename, target, code string) error {
6767
//nolint:gosec // false positive
6868
contents, err := os.ReadFile(filename)
6969
if err != nil {
70-
return err
70+
return fmt.Errorf("failed to read file %q: %w", filename, err)
7171
}
7272
idx := strings.Index(string(contents), target)
7373
if idx == -1 {
7474
return fmt.Errorf("string %s not found in %s", target, string(contents))
7575
}
7676
out := string(contents[:idx+len(target)]) + code + string(contents[idx+len(target):])
7777
//nolint:gosec // false positive
78-
return os.WriteFile(filename, []byte(out), 0o644)
78+
if errWriteFile := os.WriteFile(filename, []byte(out), 0o644); errWriteFile != nil {
79+
return fmt.Errorf("failed to write file %q: %w", filename, errWriteFile)
80+
}
81+
82+
return nil
7983
}
8084

81-
// InsertCodeIfNotExist insert code if it does not already exists
85+
// InsertCodeIfNotExist insert code if it does not already exist
8286
func InsertCodeIfNotExist(filename, target, code string) error {
8387
//nolint:gosec // false positive
8488
contents, err := os.ReadFile(filename)
8589
if err != nil {
86-
return err
90+
return fmt.Errorf("failed to read file %q: %w", filename, err)
8791
}
8892

8993
idx := strings.Index(string(contents), code)
@@ -98,7 +102,7 @@ func InsertCodeIfNotExist(filename, target, code string) error {
98102
func AppendCodeIfNotExist(filename, code string) error {
99103
contents, err := os.ReadFile(filename)
100104
if err != nil {
101-
return err
105+
return fmt.Errorf("failed to read file %q: %w", filename, err)
102106
}
103107

104108
if strings.Contains(string(contents), code) {
@@ -112,16 +116,19 @@ func AppendCodeIfNotExist(filename, code string) error {
112116
func AppendCodeAtTheEnd(filename, code string) error {
113117
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o644)
114118
if err != nil {
115-
return err
119+
return fmt.Errorf("failed to open file %q: %w", filename, err)
116120
}
117121
defer func() {
118122
if err = f.Close(); err != nil {
119123
return
120124
}
121125
}()
122126

123-
_, err = f.WriteString(code)
124-
return err
127+
if _, errWriteString := f.WriteString(code); errWriteString != nil {
128+
return fmt.Errorf("failed to write to file %q: %w", filename, errWriteString)
129+
}
130+
131+
return nil
125132
}
126133

127134
// UncommentCode searches for target in the file and remove the comment prefix
@@ -130,19 +137,19 @@ func UncommentCode(filename, target, prefix string) error {
130137
//nolint:gosec // false positive
131138
content, err := os.ReadFile(filename)
132139
if err != nil {
133-
return err
140+
return fmt.Errorf("failed to read file %q: %w", filename, err)
134141
}
135142
strContent := string(content)
136143

137144
idx := strings.Index(strContent, target)
138145
if idx < 0 {
139-
return fmt.Errorf("unable to find the code %s to be uncomment", target)
146+
return fmt.Errorf("unable to find the code %q to be uncomment", target)
140147
}
141148

142149
out := new(bytes.Buffer)
143150
_, err = out.Write(content[:idx])
144151
if err != nil {
145-
return err
152+
return fmt.Errorf("failed to write to file %q: %w", filename, err)
146153
}
147154

148155
scanner := bufio.NewScanner(bytes.NewBufferString(target))
@@ -151,23 +158,26 @@ func UncommentCode(filename, target, prefix string) error {
151158
}
152159
for {
153160
if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil {
154-
return err
161+
return fmt.Errorf("failed to write to file %q: %w", filename, err)
155162
}
156163
// Avoid writing a newline in case the previous line was the last in target.
157164
if !scanner.Scan() {
158165
break
159166
}
160167
if _, err = out.WriteString("\n"); err != nil {
161-
return err
168+
return fmt.Errorf("failed to write to file %q: %w", filename, err)
162169
}
163170
}
164171

165-
_, err = out.Write(content[idx+len(target):])
166-
if err != nil {
167-
return err
172+
if _, err = out.Write(content[idx+len(target):]); err != nil {
173+
return fmt.Errorf("failed to write to file %q: %w", filename, err)
168174
}
169175
//nolint:gosec // false positive
170-
return os.WriteFile(filename, out.Bytes(), 0o644)
176+
if err = os.WriteFile(filename, out.Bytes(), 0o644); err != nil {
177+
return fmt.Errorf("failed to write file %q: %w", filename, err)
178+
}
179+
180+
return nil
171181
}
172182

173183
// CommentCode searches for target in the file and adds the comment prefix
@@ -176,42 +186,44 @@ func CommentCode(filename, target, prefix string) error {
176186
// Read the file content
177187
content, err := os.ReadFile(filename)
178188
if err != nil {
179-
return err
189+
return fmt.Errorf("failed to read file %q: %w", filename, err)
180190
}
181191
strContent := string(content)
182192

183193
// Find the target code to be commented
184194
idx := strings.Index(strContent, target)
185195
if idx < 0 {
186-
return fmt.Errorf("unable to find the code %s to be commented", target)
196+
return fmt.Errorf("unable to find the code %q to be commented", target)
187197
}
188198

189199
// Create a buffer to hold the modified content
190200
out := new(bytes.Buffer)
191-
_, err = out.Write(content[:idx])
192-
if err != nil {
193-
return err
201+
if _, err = out.Write(content[:idx]); err != nil {
202+
return fmt.Errorf("failed to write to file %q: %w", filename, err)
194203
}
195204

196205
// Add the comment prefix to each line of the target code
197206
scanner := bufio.NewScanner(bytes.NewBufferString(target))
198207
for scanner.Scan() {
199208
if _, err = out.WriteString(prefix + scanner.Text() + "\n"); err != nil {
200-
return err
209+
return fmt.Errorf("failed to write to file %q: %w", filename, err)
201210
}
202211
}
203212

204213
// Write the rest of the file content
205-
_, err = out.Write(content[idx+len(target):])
206-
if err != nil {
207-
return err
214+
if _, err = out.Write(content[idx+len(target):]); err != nil {
215+
return fmt.Errorf("failed to write to file %q: %w", filename, err)
208216
}
209217

210218
// Write the modified content back to the file
211-
return os.WriteFile(filename, out.Bytes(), 0o644)
219+
if err = os.WriteFile(filename, out.Bytes(), 0o644); err != nil {
220+
return fmt.Errorf("failed to write file %q: %w", filename, err)
221+
}
222+
223+
return nil
212224
}
213225

214-
// EnsureExistAndReplace check if the content exists and then do the replace
226+
// EnsureExistAndReplace check if the content exists and then do the replacement
215227
func EnsureExistAndReplace(input, match, replace string) (string, error) {
216228
if !strings.Contains(input, match) {
217229
return "", fmt.Errorf("can't find %q", match)
@@ -223,20 +235,19 @@ func EnsureExistAndReplace(input, match, replace string) (string, error) {
223235
func ReplaceInFile(path, oldValue, newValue string) error {
224236
info, err := os.Stat(path)
225237
if err != nil {
226-
return err
238+
return fmt.Errorf("failed to stat file %q: %w", path, err)
227239
}
228240
//nolint:gosec // false positive
229241
b, err := os.ReadFile(path)
230242
if err != nil {
231-
return err
243+
return fmt.Errorf("failed to read file %q: %w", path, err)
232244
}
233245
if !strings.Contains(string(b), oldValue) {
234246
return errors.New("unable to find the content to be replaced")
235247
}
236248
s := strings.Replace(string(b), oldValue, newValue, -1)
237-
err = os.WriteFile(path, []byte(s), info.Mode())
238-
if err != nil {
239-
return err
249+
if err = os.WriteFile(path, []byte(s), info.Mode()); err != nil {
250+
return fmt.Errorf("failed to write file %q: %w", path, err)
240251
}
241252
return nil
242253
}
@@ -249,25 +260,26 @@ func ReplaceInFile(path, oldValue, newValue string) error {
249260
func ReplaceRegexInFile(path, match, replace string) error {
250261
matcher, err := regexp.Compile(match)
251262
if err != nil {
252-
return err
263+
return fmt.Errorf("failed to compile regular expression %q: %w", match, err)
253264
}
254265
info, err := os.Stat(path)
255266
if err != nil {
256-
return err
267+
return fmt.Errorf("failed to stat file %q: %w", path, err)
257268
}
258269
//nolint:gosec // false positive
259270
b, err := os.ReadFile(path)
260271
if err != nil {
261-
return err
272+
return fmt.Errorf("failed to read file %q: %w", path, err)
262273
}
263274
s := matcher.ReplaceAllString(string(b), replace)
264275
if s == string(b) {
265276
return errors.New("unable to find the content to be replaced")
266277
}
267-
err = os.WriteFile(path, []byte(s), info.Mode())
268-
if err != nil {
269-
return err
278+
279+
if err = os.WriteFile(path, []byte(s), info.Mode()); err != nil {
280+
return fmt.Errorf("failed to write file %q: %w", path, err)
270281
}
282+
271283
return nil
272284
}
273285

@@ -276,7 +288,7 @@ func HasFileContentWith(path, text string) (bool, error) {
276288
//nolint:gosec
277289
contents, err := os.ReadFile(path)
278290
if err != nil {
279-
return false, err
291+
return false, fmt.Errorf("failed to read file %q: %w", path, err)
280292
}
281293

282294
return strings.Contains(string(contents), text), nil

pkg/plugin/version.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ func (v *Version) Parse(version string) error {
5454
if n, errParse := strconv.Atoi(version); errParse == nil && n < 0 {
5555
return errNegative
5656
}
57-
return err
57+
return fmt.Errorf("error converting version number %q: %w", substrings[0], err)
5858
}
5959

6060
if len(substrings) > 1 {
6161
if err = v.Stage.Parse(substrings[1]); err != nil {
62-
return err
62+
return fmt.Errorf("error parsing stage %q: %w", substrings[1], err)
6363
}
6464
}
6565

@@ -81,7 +81,11 @@ func (v Version) Validate() error {
8181
return errNegative
8282
}
8383

84-
return v.Stage.Validate()
84+
if err := v.Stage.Validate(); err != nil {
85+
return fmt.Errorf("error validating stage %q: %w", v.Stage, err)
86+
}
87+
88+
return nil
8589
}
8690

8791
// Compare returns -1 if v < other, 0 if v == other, and 1 if v > other.

0 commit comments

Comments
 (0)