Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return only the raw evaluation error message without prefix #1726

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions internal/engine/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,32 @@ import (
"github.com/stacklok/minder/internal/db"
)

// EvaluationError is a custom error type for evaluation errors.
type EvaluationError struct {
Base error
Msg string
}

// Unwrap returns the base error, allowing errors.Is to work with wrapped errors.
func (e *EvaluationError) Unwrap() error {
return e.Base
}

// Error implements the error interface for EvaluationError.
func (e *EvaluationError) Error() string {
return fmt.Sprintf("%v: %s", e.Base, e.Msg)
}

// ErrEvaluationFailed is an error that occurs during evaluation of a rule.
var ErrEvaluationFailed = errors.New("evaluation failure")

// NewErrEvaluationFailed creates a new evaluation error
// NewErrEvaluationFailed creates a new evaluation error with a formatted message.
func NewErrEvaluationFailed(sfmt string, args ...any) error {
msg := fmt.Sprintf(sfmt, args...)
return fmt.Errorf("%w: %s", ErrEvaluationFailed, msg)
return &EvaluationError{
Base: ErrEvaluationFailed,
Msg: msg,
}
}

// ErrEvaluationSkipped specifies that the rule was evaluated but skipped.
Expand Down Expand Up @@ -102,7 +121,10 @@ func ErrorAsEvalStatus(err error) db.EvalStatusTypes {

// ErrorAsEvalDetails returns the evaluation details for a given error
func ErrorAsEvalDetails(err error) string {
if err != nil {
var evalErr *EvaluationError
if errors.As(err, &evalErr) {
return evalErr.Msg
} else if err != nil {
return err.Error()
}

Expand Down
Loading