Skip to content

Commit

Permalink
Use bundle_error code to detect error scenarios.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushpalanka committed Jul 17, 2024
1 parent d8d6898 commit d32f5f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
36 changes: 27 additions & 9 deletions filters/openpolicyagent/openpolicyagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,22 +832,21 @@ func handleStatusErrors(
failed chan error,
prefix string,
) {
if status.Code != "" || len(status.Errors) > 0 { //ToDo refine error handling to cover all the cases with detailed messages
if status.Code == "bundle_error" {
if status.HTTPCode == "" {
failed <- fmt.Errorf("%s failed: %s", prefix, status.Code)
failed <- formatStatusError(prefix, status)
return
}
code, err := status.HTTPCode.Int64()
if err == nil && (code >= 400 && code < 500) && !isTemporaryError(code) {
if err == nil && code >= 400 && code < 500 && !isTemporaryError(code) {
// Fail for error codes in the range 400-500 excluding temporary errors
failed <- fmt.Errorf("%s failed: %s", prefix, status.Code)
failed <- formatStatusError(prefix, status)
return
}
if err != nil {
failed <- formatStatusError(prefix, status)
return
}
}

if len(status.Errors) > 0 {
failed <- fmt.Errorf("%s failed: %w", prefix, errors.Join(status.Errors...))
return
}
}

Expand All @@ -859,3 +858,22 @@ func isTemporaryError(code int64) bool {
}
return false
}

func formatStatusError(prefix string, status bundle.Status) error {
var b strings.Builder
b.WriteString(fmt.Sprintf("%s failed:", prefix))
b.WriteString(fmt.Sprintf(" Name: %s", status.Name))
if status.Code != "" {
b.WriteString(fmt.Sprintf(", Code: %s", status.Code))
}
if status.Message != "" {
b.WriteString(fmt.Sprintf(", Message: %s", status.Message))
}
if status.HTTPCode != "" {
b.WriteString(fmt.Sprintf(", HTTPCode: %s", status.HTTPCode))
}
if len(status.Errors) > 0 {
b.WriteString(fmt.Sprintf(", Errors: %v", status.Errors))
}
return errors.New(b.String())
}
6 changes: 3 additions & 3 deletions filters/openpolicyagent/openpolicyagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestOpaEngineStartFailureWithWrongBundle(t *testing.T) {

err = engine.Start(ctx, cfg.startupTimeout)
assert.True(t, engine.stopped)
assert.Contains(t, err.Error(), "bundle plugin failed: bundle_error")
assert.Contains(t, err.Error(), "bundle plugin failed: Name: bundles/non-existing-bundle, Code: bundle_error, Message: server replied with Not Found, HTTPCode: 404")
}

func TestOpaActivationSuccessWithDiscovery(t *testing.T) {
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestOpaActivationTimeOutWithDiscoveryPointingWrongBundle(t *testing.T) {

instance, err := registry.NewOpenPolicyAgentInstance("test", *cfg, "testfilter")
assert.Nil(t, instance)
assert.Contains(t, err.Error(), "bundle plugin failed: bundle_error")
assert.Contains(t, err.Error(), "bundle plugin failed: Name: bundles/non-existing-bundle, Code: bundle_error, Message: server replied with Not Found, HTTPCode: 404")
assert.Equal(t, 0, len(registry.instances))
}

Expand Down Expand Up @@ -707,5 +707,5 @@ func TestOpaActivationFailureWithInvalidDiscovery(t *testing.T) {

_, err = registry.NewOpenPolicyAgentInstance("test", *cfg, "testfilter")
assert.Error(t, err)
assert.Equal(t, "discovery plugin failed: bundle_error", err.Error())
assert.Equal(t, "discovery plugin failed: Name: discovery, Code: bundle_error, Message: server replied with Not Found, HTTPCode: 404", err.Error())
}

0 comments on commit d32f5f3

Please sign in to comment.