Skip to content

Commit 173a050

Browse files
Piskoomigmartri
andauthored
feat(policy): improve error messages (#2345)
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com> Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev> Co-authored-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent ecce569 commit 173a050

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

app/cli/cmd/policy_develop_eval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ evaluates the policy against the provided material or attestation.`,
5555

5656
policyEval, err := action.NewPolicyEval(opts, actionOpts)
5757
if err != nil {
58-
return fmt.Errorf("failed to initialize policy evaluation: %w", err)
58+
return err
5959
}
6060

6161
result, err := policyEval.Run()

app/cli/cmd/policy_develop_lint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func newPolicyDevelopLintCmd() *cobra.Command {
4040
RunE: func(cmd *cobra.Command, _ []string) error {
4141
a, err := action.NewPolicyLint(actionOpts)
4242
if err != nil {
43-
return fmt.Errorf("failed to initialize linter: %w", err)
43+
return err
4444
}
4545

4646
result, err := a.Run(cmd.Context(), &action.PolicyLintOpts{
@@ -49,7 +49,7 @@ func newPolicyDevelopLintCmd() *cobra.Command {
4949
RegalConfig: regalConfig,
5050
})
5151
if err != nil {
52-
return fmt.Errorf("linting policy: %w", err)
52+
return err
5353
}
5454

5555
if result.Valid {

app/cli/internal/action/policy_develop_eval.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package action
1717

1818
import (
19-
"fmt"
20-
2119
"github.com/chainloop-dev/chainloop/app/cli/internal/policydevel"
2220
)
2321

@@ -62,7 +60,7 @@ func (action *PolicyEval) Run() ([]*PolicyEvalResult, error) {
6260
// Evaluate policy
6361
resp, err := policydevel.Evaluate(evalOpts, action.Logger)
6462
if err != nil {
65-
return nil, fmt.Errorf("evaluating policy: %w", err)
63+
return nil, err
6664
}
6765

6866
results := make([]*PolicyEvalResult, 0, len(resp))

app/cli/internal/action/policy_develop_lint.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package action
1717

1818
import (
1919
"context"
20-
"fmt"
2120

2221
"github.com/chainloop-dev/chainloop/app/cli/internal/policydevel"
2322
)
@@ -47,7 +46,7 @@ func (action *PolicyLint) Run(_ context.Context, opts *PolicyLintOpts) (*PolicyL
4746
// Read policies
4847
policy, err := policydevel.Lookup(opts.PolicyPath, opts.RegalConfig, opts.Format)
4948
if err != nil {
50-
return nil, fmt.Errorf("loading policy: %w", err)
49+
return nil, err
5150
}
5251

5352
// Run all validations

app/cli/internal/policydevel/lint.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ func (p *PolicyToLint) AddError(path, message string, line int) {
8585
func Lookup(absPath, config string, format bool) (*PolicyToLint, error) {
8686
resolvedPath, err := resourceloader.GetPathForResource(absPath)
8787
if err != nil {
88-
return nil, fmt.Errorf("failed to resolve policy file: %w", err)
88+
return nil, err
8989
}
9090

9191
fileInfo, err := os.Stat(resolvedPath)
9292
if err != nil {
9393
if os.IsNotExist(err) {
9494
return nil, fmt.Errorf("policy file does not exist: %s", resolvedPath)
9595
}
96-
return nil, fmt.Errorf("failed to stat file %q: %w", resolvedPath, err)
96+
return nil, err
9797
}
9898
if fileInfo.IsDir() {
9999
return nil, fmt.Errorf("expected a file but got a directory: %s", resolvedPath)
@@ -142,14 +142,14 @@ func (p *PolicyToLint) loadReferencedRegoFiles(baseDir string) error {
142142

143143
resolvedPath, err := resourceloader.GetPathForResource(regoPath)
144144
if err != nil {
145-
return fmt.Errorf("failed to resolve rego file %q: %w", regoPath, err)
145+
return err
146146
}
147147
if _, ok := seen[resolvedPath]; ok {
148148
continue // avoid duplicates
149149
}
150150
seen[resolvedPath] = struct{}{}
151151
if err := p.processFile(resolvedPath); err != nil {
152-
return fmt.Errorf("failed to load referenced rego file %q: %w", resolvedPath, err)
152+
return err
153153
}
154154
}
155155
}
@@ -160,7 +160,7 @@ func (p *PolicyToLint) loadReferencedRegoFiles(baseDir string) error {
160160
func (p *PolicyToLint) processFile(filePath string) error {
161161
content, err := os.ReadFile(filePath)
162162
if err != nil {
163-
return fmt.Errorf("reading %s: %w", filepath.Base(filePath), err)
163+
return err
164164
}
165165

166166
ext := strings.ToLower(filepath.Ext(filePath))
@@ -197,7 +197,7 @@ func (p *PolicyToLint) Validate() {
197197
func (p *PolicyToLint) validateYAMLFile(file *File) {
198198
var policy v1.Policy
199199
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)
201201
return
202202
}
203203

@@ -207,7 +207,7 @@ func (p *PolicyToLint) validateYAMLFile(file *File) {
207207
if p.Format {
208208
var root yaml.Node
209209
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)
211211
return
212212
}
213213

@@ -222,13 +222,13 @@ func (p *PolicyToLint) validateYAMLFile(file *File) {
222222
defer enc.Close()
223223

224224
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)
226226
return
227227
}
228228

229229
outYAML := buf.Bytes()
230230
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)
232232
} else {
233233
if err := os.WriteFile(file.Path, outYAML, 0600); err != nil {
234234
p.AddError(file.Path, fmt.Sprintf("failed to save updated file: %v", err), 0)
@@ -260,7 +260,7 @@ func (p *PolicyToLint) validateRegoFile(file *File) {
260260

261261
if p.Format && formatted != original {
262262
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)
264264
} else {
265265
file.Content = []byte(formatted)
266266
}
@@ -286,7 +286,7 @@ func (p *PolicyToLint) validateAndFormatRego(content, path string) string {
286286
func (p *PolicyToLint) applyOPAFmt(content, file string) string {
287287
formatted, err := format.SourceWithOpts(file, []byte(content), format.Opts{})
288288
if err != nil {
289-
p.AddError(file, "Auto-formatting failed", 0)
289+
p.AddError(file, "auto-formatting failed", 0)
290290
return content
291291
}
292292
return string(formatted)
@@ -352,7 +352,7 @@ func (p *PolicyToLint) runRegalLinter(filePath, content string) {
352352

353353
report, err := lntr.Lint(context.Background())
354354
if err != nil {
355-
p.AddError(filePath, fmt.Sprintf("linting failed: %v", err), 0)
355+
p.AddError(filePath, err.Error(), 0)
356356
return
357357
}
358358

0 commit comments

Comments
 (0)