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

runner: Deprecate EnsureNoError helper #236

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions helper/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ func (r *Runner) EmitIssue(rule tflint.Rule, message string, location hcl.Range)
}

// EnsureNoError is a method that simply runs a function if there is no error.
//
// Deprecated: Use errors.Is() instead to determine which errors can be ignored.
func (r *Runner) EnsureNoError(err error, proc func() error) error {
if err == nil {
return proc()
Expand Down
2 changes: 2 additions & 0 deletions plugin/plugin2host/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ func (c *GRPCClient) EmitIssue(rule tflint.Rule, message string, location hcl.Ra

// EnsureNoError is a helper for error handling. Depending on the type of error generated by EvaluateExpr,
// determine whether to exit, skip, or continue. If it is continued, the passed function will be executed.
//
// Deprecated: Use errors.Is() instead to determine which errors can be ignored.
func (*GRPCClient) EnsureNoError(err error, proc func() error) error {
if err == nil {
return proc()
Expand Down
11 changes: 5 additions & 6 deletions tflint/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
)

// List of errors returned by TFLint.
// It's possible to get this error from a plugin, but the error handling is hidden
// inside the plugin system, so you usually don't have to worry about it.
var (
// ErrUnknownValue is an error when an unknown value is referenced
// ErrUnknownValue is an error that occurs when decoding an unknown value to a Go value.
ErrUnknownValue = errors.New("unknown value found")
// ErrNullValue is an error when null value is referenced
// ErrNullValue is an error that occurs when decoding null to a Go value.
ErrNullValue = errors.New("null value found")
// ErrUnevaluable is an error when a received expression has unevaluable references.
// ErrUnevaluable is an error that occurs when decoding an unevaluable value to a Go value.
//
// Deprecated: This error is no longer returned since TFLint v0.41.
ErrUnevaluable = errors.New("")
// ErrSensitive is an error when a received expression contains a sensitive value.
// ErrSensitive is an error that occurs when decoding a sensitive value to a Go value.
ErrSensitive = errors.New("")
)
27 changes: 19 additions & 8 deletions tflint/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,29 @@ type Runner interface {

// EvaluateExpr evaluates the passed expression and reflects the result in the 2nd argument.
// In addition to the obvious errors, this function returns an error if:
// - The expression contains unknown variables (e.g. variables without defaults)
// - The expression contains null variables
// - The expression contains unevaluable references (e.g. `aws_instance.arn`)
// - The expression contains unknown values (e.g. variables without defaults, `aws_instance.foo.arn`)
// - The expression contains null values
// - The expression contains sensitive values (variables with `sensitive = true`)
//
// To ignore these, use EnsureNoError for the returned error:
// You can use `errors.Is` to ignore these errors:
//
// ```
// var val string
// err := runner.EvaluateExpr(expr, &val, nil)
// err = runner.EnsureNoError(err, func () error {
// // Only when no error occurs
// })
// if err != nil {
// // Only for obvious errors, excluding the above errors
// if errors.Is(err, tflint.ErrUnknownValue) {
// // Ignore unknown values
// return nil
// }
// if errors.Is(err, tflint.ErrNullValue) {
// // Ignore null values because null means that the value is not set
// return nil
// }
// if errors.Is(err, tflint.ErrSensitive) {
// // Ignore sensitive values
// return nil
// }
// return err
// }
// ```
//
Expand Down Expand Up @@ -211,6 +220,8 @@ type Runner interface {

// EnsureNoError is a helper for error handling. Depending on the type of error generated by EvaluateExpr,
// determine whether to exit, skip, or continue. If it is continued, the passed function will be executed.
//
// Deprecated: Use errors.Is() instead to determine which errors can be ignored.
EnsureNoError(error, func() error) error
}

Expand Down