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

check error type instead of string #145

Merged

Conversation

ctab
Copy link
Member

@ctab ctab commented Jun 19, 2019

No description provided.

@ctab ctab force-pushed the check_for_unrecognized_template_type branch from 6ee54c2 to a90bebc Compare June 19, 2019 19:10
@ctab ctab requested a review from maxsmythe June 19, 2019 19:24
@@ -130,8 +129,7 @@ func (r *ReconcileConstraint) Reconcile(request reconcile.Request) (reconcile.Re
// Handle deletion
if containsString(finalizerName, instance.GetFinalizers()) {
if _, err := r.opa.RemoveConstraint(context.Background(), instance); err != nil {
// TODO make the unrecognized constraint error detectable via class introspection
if !(strings.HasPrefix(err.Error(), "Constraint kind ") && strings.HasSuffix(err.Error(), " is not recognized")) {
if reflect.TypeOf(err).String() == "client.UnrecognizedConstraintError" {
Copy link
Contributor

@maxsmythe maxsmythe Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type switching would be the better approach (no strings):

if _, ok := err.(client.UnrecognizedConstraintError); ok {

}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @maxsmythe - just changed it

Signed-off-by: Craig Tabita <ctab@google.com>
@@ -130,8 +128,7 @@ func (r *ReconcileConstraint) Reconcile(request reconcile.Request) (reconcile.Re
// Handle deletion
if containsString(finalizerName, instance.GetFinalizers()) {
if _, err := r.opa.RemoveConstraint(context.Background(), instance); err != nil {
// TODO make the unrecognized constraint error detectable via class introspection
if !(strings.HasPrefix(err.Error(), "Constraint kind ") && strings.HasSuffix(err.Error(), " is not recognized")) {
if _, ok := err.(*opa.UnrecognizedConstraintError); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently opa.UnrecognizedConstraintError is a value, not a pointer, so this type check would never be true.

We should actually update the frameworks library to use a pointer-to-a-string, much like:

https://golang.org/src/errors/errors.go

ctab and others added 3 commits June 21, 2019 10:55
Signed-off-by: Craig Tabita <ctab@google.com>
…b/gatekeeper into check_for_unrecognized_template_type

Signed-off-by: Craig Tabita <ctab@google.com>
@maxsmythe maxsmythe merged commit 623dc71 into open-policy-agent:master Jun 21, 2019
@ctab ctab deleted the check_for_unrecognized_template_type branch June 26, 2019 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants