mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
make Err wrap one or zero error.
This commit is contained in:
parent
b846c39047
commit
ce45a82346
@ -163,12 +163,12 @@ type variableDeclEnvs map[OptionalVariableDeclarations]*environment.EnvSet
|
||||
// CompileCELExpression returns a compiled CEL expression.
|
||||
// perCallLimit was added for testing purpose only. Callers should always use const PerCallLimit from k8s.io/apiserver/pkg/apis/cel/config.go as input.
|
||||
func (c compiler) CompileCELExpression(expressionAccessor ExpressionAccessor, options OptionalVariableDeclarations, envType environment.Type) CompilationResult {
|
||||
resultError := func(errorString string, errType apiservercel.ErrorType, errors ...error) CompilationResult {
|
||||
resultError := func(errorString string, errType apiservercel.ErrorType, cause error) CompilationResult {
|
||||
return CompilationResult{
|
||||
Error: &apiservercel.Error{
|
||||
Type: errType,
|
||||
Detail: errorString,
|
||||
Errors: errors,
|
||||
Cause: cause,
|
||||
},
|
||||
ExpressionAccessor: expressionAccessor,
|
||||
}
|
||||
@ -176,7 +176,7 @@ func (c compiler) CompileCELExpression(expressionAccessor ExpressionAccessor, op
|
||||
|
||||
env, err := c.varEnvs[options].Env(envType)
|
||||
if err != nil {
|
||||
return resultError(fmt.Sprintf("unexpected error loading CEL environment: %v", err), apiservercel.ErrorTypeInternal)
|
||||
return resultError(fmt.Sprintf("unexpected error loading CEL environment: %v", err), apiservercel.ErrorTypeInternal, nil)
|
||||
}
|
||||
|
||||
ast, issues := env.Compile(expressionAccessor.GetExpression())
|
||||
@ -199,19 +199,19 @@ func (c compiler) CompileCELExpression(expressionAccessor ExpressionAccessor, op
|
||||
reason = fmt.Sprintf("must evaluate to one of %v", returnTypes)
|
||||
}
|
||||
|
||||
return resultError(reason, apiservercel.ErrorTypeInvalid)
|
||||
return resultError(reason, apiservercel.ErrorTypeInvalid, nil)
|
||||
}
|
||||
|
||||
_, err = cel.AstToCheckedExpr(ast)
|
||||
if err != nil {
|
||||
// should be impossible since env.Compile returned no issues
|
||||
return resultError("unexpected compilation error: "+err.Error(), apiservercel.ErrorTypeInternal)
|
||||
return resultError("unexpected compilation error: "+err.Error(), apiservercel.ErrorTypeInternal, nil)
|
||||
}
|
||||
prog, err := env.Program(ast,
|
||||
cel.InterruptCheckFrequency(celconfig.CheckFrequency),
|
||||
)
|
||||
if err != nil {
|
||||
return resultError("program instantiation failed: "+err.Error(), apiservercel.ErrorTypeInternal)
|
||||
return resultError("program instantiation failed: "+err.Error(), apiservercel.ErrorTypeInternal, nil)
|
||||
}
|
||||
return CompilationResult{
|
||||
Program: prog,
|
||||
|
@ -192,7 +192,7 @@ func (f *filter) ForInput(ctx context.Context, versionedAttr *admission.Versione
|
||||
evaluation.Error = &cel.Error{
|
||||
Type: cel.ErrorTypeInvalid,
|
||||
Detail: fmt.Sprintf("compilation error: %v", compilationResult.Error),
|
||||
Errors: []error{compilationResult.Error},
|
||||
Cause: compilationResult.Error,
|
||||
}
|
||||
continue
|
||||
}
|
||||
@ -212,7 +212,7 @@ func (f *filter) ForInput(ctx context.Context, versionedAttr *admission.Versione
|
||||
return nil, -1, &cel.Error{
|
||||
Type: cel.ErrorTypeInvalid,
|
||||
Detail: fmt.Sprintf("validation failed due to running out of cost budget, no further validation rules will be run"),
|
||||
Errors: []error{cel.ErrOutOfBudget},
|
||||
Cause: cel.ErrOutOfBudget,
|
||||
}
|
||||
}
|
||||
remainingBudget -= compositionCost
|
||||
@ -230,14 +230,14 @@ func (f *filter) ForInput(ctx context.Context, versionedAttr *admission.Versione
|
||||
return nil, -1, &cel.Error{
|
||||
Type: cel.ErrorTypeInvalid,
|
||||
Detail: fmt.Sprintf("runtime cost could not be calculated for expression: %v, no further expression will be run", compilationResult.ExpressionAccessor.GetExpression()),
|
||||
Errors: []error{cel.ErrOutOfBudget},
|
||||
Cause: cel.ErrOutOfBudget,
|
||||
}
|
||||
} else {
|
||||
if *rtCost > math.MaxInt64 || int64(*rtCost) > remainingBudget {
|
||||
return nil, -1, &cel.Error{
|
||||
Type: cel.ErrorTypeInvalid,
|
||||
Detail: fmt.Sprintf("validation failed due to running out of cost budget, no further validation rules will be run"),
|
||||
Errors: []error{cel.ErrOutOfBudget},
|
||||
Cause: cel.ErrOutOfBudget,
|
||||
}
|
||||
}
|
||||
remainingBudget -= int64(*rtCost)
|
||||
|
@ -53,7 +53,7 @@ func (f *fakeCelFilter) ForInput(ctx context.Context, versionedAttr *admission.V
|
||||
return nil, -1, &apiservercel.Error{
|
||||
Type: apiservercel.ErrorTypeInvalid,
|
||||
Detail: fmt.Sprintf("validation failed due to running out of cost budget, no further validation rules will be run"),
|
||||
Errors: []error{apiservercel.ErrOutOfBudget},
|
||||
Cause: apiservercel.ErrOutOfBudget,
|
||||
}
|
||||
}
|
||||
if f.throwError {
|
||||
|
@ -53,9 +53,9 @@ type Error struct {
|
||||
Type ErrorType
|
||||
Detail string
|
||||
|
||||
// Errors are optional wrapped errors that can be useful to
|
||||
// Cause is an optional wrapped errors that can be useful to
|
||||
// programmatically retrieve detailed errors.
|
||||
Errors []error
|
||||
Cause error
|
||||
}
|
||||
|
||||
var _ error = &Error{}
|
||||
@ -77,9 +77,9 @@ func (v *Error) Is(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Unwrap returns wrapped errors.
|
||||
func (v *Error) Unwrap() []error {
|
||||
return v.Errors
|
||||
// Unwrap returns the wrapped Cause.
|
||||
func (v *Error) Unwrap() error {
|
||||
return v.Cause
|
||||
}
|
||||
|
||||
// ErrorType is a machine-readable value providing more detail about why
|
||||
|
@ -27,7 +27,7 @@ func TestOutOfBudgetError(t *testing.T) {
|
||||
err := &Error{
|
||||
Type: ErrorTypeInvalid,
|
||||
Detail: "expression out of budget",
|
||||
Errors: []error{ErrOutOfBudget},
|
||||
Cause: ErrOutOfBudget,
|
||||
}
|
||||
if !errors.Is(err, ErrOutOfBudget) {
|
||||
t.Errorf("unexpected %v is not %v", err, ErrOutOfBudget)
|
||||
@ -45,7 +45,7 @@ func TestCompilationError(t *testing.T) {
|
||||
err := &Error{
|
||||
Type: ErrorTypeInvalid,
|
||||
Detail: "fake compilation failed",
|
||||
Errors: []error{NewCompilationError(issues)},
|
||||
Cause: NewCompilationError(issues),
|
||||
}
|
||||
if !errors.Is(err, ErrCompilation) {
|
||||
t.Errorf("unexpected %v is not %v", err, ErrCompilation)
|
||||
|
Loading…
Reference in New Issue
Block a user