@@ -85,15 +85,15 @@ func (p *PolicyToLint) AddError(path, message string, line int) {
85
85
func Lookup (absPath , config string , format bool ) (* PolicyToLint , error ) {
86
86
resolvedPath , err := resourceloader .GetPathForResource (absPath )
87
87
if err != nil {
88
- return nil , fmt . Errorf ( "failed to resolve policy file: %w" , err )
88
+ return nil , err
89
89
}
90
90
91
91
fileInfo , err := os .Stat (resolvedPath )
92
92
if err != nil {
93
93
if os .IsNotExist (err ) {
94
94
return nil , fmt .Errorf ("policy file does not exist: %s" , resolvedPath )
95
95
}
96
- return nil , fmt . Errorf ( "failed to stat file %q: %w" , resolvedPath , err )
96
+ return nil , err
97
97
}
98
98
if fileInfo .IsDir () {
99
99
return nil , fmt .Errorf ("expected a file but got a directory: %s" , resolvedPath )
@@ -142,14 +142,14 @@ func (p *PolicyToLint) loadReferencedRegoFiles(baseDir string) error {
142
142
143
143
resolvedPath , err := resourceloader .GetPathForResource (regoPath )
144
144
if err != nil {
145
- return fmt . Errorf ( "failed to resolve rego file %q: %w" , regoPath , err )
145
+ return err
146
146
}
147
147
if _ , ok := seen [resolvedPath ]; ok {
148
148
continue // avoid duplicates
149
149
}
150
150
seen [resolvedPath ] = struct {}{}
151
151
if err := p .processFile (resolvedPath ); err != nil {
152
- return fmt . Errorf ( "failed to load referenced rego file %q: %w" , resolvedPath , err )
152
+ return err
153
153
}
154
154
}
155
155
}
@@ -160,7 +160,7 @@ func (p *PolicyToLint) loadReferencedRegoFiles(baseDir string) error {
160
160
func (p * PolicyToLint ) processFile (filePath string ) error {
161
161
content , err := os .ReadFile (filePath )
162
162
if err != nil {
163
- return fmt . Errorf ( "reading %s: %w" , filepath . Base ( filePath ), err )
163
+ return err
164
164
}
165
165
166
166
ext := strings .ToLower (filepath .Ext (filePath ))
@@ -197,7 +197,7 @@ func (p *PolicyToLint) Validate() {
197
197
func (p * PolicyToLint ) validateYAMLFile (file * File ) {
198
198
var policy v1.Policy
199
199
if err := unmarshal .FromRaw (file .Content , unmarshal .RawFormatYAML , & policy , true ); err != nil {
200
- p .AddError (file .Path , fmt . Sprintf ( "failed to parse/validate: %v" , err ) , 0 )
200
+ p .AddError (file .Path , "failed to parse/validate policy" , 0 )
201
201
return
202
202
}
203
203
@@ -207,7 +207,7 @@ func (p *PolicyToLint) validateYAMLFile(file *File) {
207
207
if p .Format {
208
208
var root yaml.Node
209
209
if err := yaml .Unmarshal (file .Content , & root ); err != nil {
210
- p .AddError (file .Path , fmt . Sprintf ( "failed to parse YAML: %v" , err ) , 0 )
210
+ p .AddError (file .Path , "failed to parse YAML" , 0 )
211
211
return
212
212
}
213
213
@@ -222,13 +222,13 @@ func (p *PolicyToLint) validateYAMLFile(file *File) {
222
222
defer enc .Close ()
223
223
224
224
if err := enc .Encode (& root ); err != nil {
225
- p .AddError (file .Path , fmt . Sprintf ( "failed to encode YAML: %v" , err ), 0 )
225
+ p .AddError (file .Path , err . Error ( ), 0 )
226
226
return
227
227
}
228
228
229
229
outYAML := buf .Bytes ()
230
230
if err := os .WriteFile (file .Path , outYAML , 0600 ); err != nil {
231
- p .AddError (file .Path , fmt . Sprintf ( "failed to write updated file: %v" , err ), 0 )
231
+ p .AddError (file .Path , err . Error ( ), 0 )
232
232
} else {
233
233
if err := os .WriteFile (file .Path , outYAML , 0600 ); err != nil {
234
234
p .AddError (file .Path , fmt .Sprintf ("failed to save updated file: %v" , err ), 0 )
@@ -260,7 +260,7 @@ func (p *PolicyToLint) validateRegoFile(file *File) {
260
260
261
261
if p .Format && formatted != original {
262
262
if err := os .WriteFile (file .Path , []byte (formatted ), 0600 ); err != nil {
263
- p .AddError (file .Path , fmt . Sprintf ( "failed to auto-format: %v" , err ), 0 )
263
+ p .AddError (file .Path , err . Error ( ), 0 )
264
264
} else {
265
265
file .Content = []byte (formatted )
266
266
}
@@ -286,7 +286,7 @@ func (p *PolicyToLint) validateAndFormatRego(content, path string) string {
286
286
func (p * PolicyToLint ) applyOPAFmt (content , file string ) string {
287
287
formatted , err := format .SourceWithOpts (file , []byte (content ), format.Opts {})
288
288
if err != nil {
289
- p .AddError (file , "Auto -formatting failed" , 0 )
289
+ p .AddError (file , "auto -formatting failed" , 0 )
290
290
return content
291
291
}
292
292
return string (formatted )
@@ -352,7 +352,7 @@ func (p *PolicyToLint) runRegalLinter(filePath, content string) {
352
352
353
353
report , err := lntr .Lint (context .Background ())
354
354
if err != nil {
355
- p .AddError (filePath , fmt . Sprintf ( "linting failed: %v" , err ), 0 )
355
+ p .AddError (filePath , err . Error ( ), 0 )
356
356
return
357
357
}
358
358
0 commit comments