Skip to content

Commit d32f5f3

Browse files
committed
Use bundle_error code to detect error scenarios.
1 parent d8d6898 commit d32f5f3

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

filters/openpolicyagent/openpolicyagent.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -832,22 +832,21 @@ func handleStatusErrors(
832832
failed chan error,
833833
prefix string,
834834
) {
835-
if status.Code != "" || len(status.Errors) > 0 { //ToDo refine error handling to cover all the cases with detailed messages
835+
if status.Code == "bundle_error" {
836836
if status.HTTPCode == "" {
837-
failed <- fmt.Errorf("%s failed: %s", prefix, status.Code)
837+
failed <- formatStatusError(prefix, status)
838838
return
839839
}
840840
code, err := status.HTTPCode.Int64()
841-
if err == nil && (code >= 400 && code < 500) && !isTemporaryError(code) {
841+
if err == nil && code >= 400 && code < 500 && !isTemporaryError(code) {
842842
// Fail for error codes in the range 400-500 excluding temporary errors
843-
failed <- fmt.Errorf("%s failed: %s", prefix, status.Code)
843+
failed <- formatStatusError(prefix, status)
844+
return
845+
}
846+
if err != nil {
847+
failed <- formatStatusError(prefix, status)
844848
return
845849
}
846-
}
847-
848-
if len(status.Errors) > 0 {
849-
failed <- fmt.Errorf("%s failed: %w", prefix, errors.Join(status.Errors...))
850-
return
851850
}
852851
}
853852

@@ -859,3 +858,22 @@ func isTemporaryError(code int64) bool {
859858
}
860859
return false
861860
}
861+
862+
func formatStatusError(prefix string, status bundle.Status) error {
863+
var b strings.Builder
864+
b.WriteString(fmt.Sprintf("%s failed:", prefix))
865+
b.WriteString(fmt.Sprintf(" Name: %s", status.Name))
866+
if status.Code != "" {
867+
b.WriteString(fmt.Sprintf(", Code: %s", status.Code))
868+
}
869+
if status.Message != "" {
870+
b.WriteString(fmt.Sprintf(", Message: %s", status.Message))
871+
}
872+
if status.HTTPCode != "" {
873+
b.WriteString(fmt.Sprintf(", HTTPCode: %s", status.HTTPCode))
874+
}
875+
if len(status.Errors) > 0 {
876+
b.WriteString(fmt.Sprintf(", Errors: %v", status.Errors))
877+
}
878+
return errors.New(b.String())
879+
}

filters/openpolicyagent/openpolicyagent_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestOpaEngineStartFailureWithWrongBundle(t *testing.T) {
258258

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

264264
func TestOpaActivationSuccessWithDiscovery(t *testing.T) {
@@ -339,7 +339,7 @@ func TestOpaActivationTimeOutWithDiscoveryPointingWrongBundle(t *testing.T) {
339339

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

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

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

0 commit comments

Comments
 (0)