-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable direct error handling for bundle plugin by returning downloade…
…r errors for bundles in manual trigger mode Signed-off-by: Torsten Wunderlich <[email protected]>
- Loading branch information
Showing
5 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package bundle | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Errors represents a list of errors that occurred during a bundle load enriched by the bundle name. | ||
type Errors []Error | ||
|
||
func (e Errors) Unwrap() []error { | ||
output := make([]error, len(e)) | ||
for i := range e { | ||
output[i] = e[i] | ||
} | ||
return output | ||
} | ||
func (e Errors) Error() string { | ||
err := errors.Join(e.Unwrap()...) | ||
return err.Error() | ||
} | ||
|
||
type Error struct { | ||
Name string | ||
Err error | ||
} | ||
|
||
func (e Error) Error() string { | ||
return fmt.Sprintf("'%s': %v", e.Name, e.Err) | ||
} | ||
|
||
func (e Error) Unwrap() error { | ||
return e.Err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package bundle | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/download" | ||
) | ||
|
||
func TestErrors(t *testing.T) { | ||
errs := Errors{ | ||
Error{Name: "foo", Err: fmt.Errorf("foo error")}, | ||
Error{Name: "bar", Err: fmt.Errorf("bar error")}, | ||
} | ||
|
||
expected := "'foo': foo error\n'bar': bar error" | ||
result := errs.Error() | ||
|
||
if result != expected { | ||
t.Errorf("Expected: %v \nbut got: %v", expected, result) | ||
} | ||
} | ||
|
||
func TestUnwrapSlice(t *testing.T) { | ||
fooErr := Error{Name: "foo", Err: fmt.Errorf("foo error")} | ||
barErr := Error{Name: "bar", Err: fmt.Errorf("bar error")} | ||
|
||
errs := Errors{fooErr, barErr} | ||
|
||
result := errs.Unwrap() | ||
|
||
if result[0].Error() != fooErr.Error() { | ||
t.Fatalf("expected %v \nbut got: %v", fooErr, result[0]) | ||
} | ||
if result[1].Error() != barErr.Error() { | ||
t.Fatalf("expected %v \nbut got: %v", barErr, result[1]) | ||
} | ||
} | ||
|
||
func TestUnwrap(t *testing.T) { | ||
fooErr := Error{Name: "foo", Err: download.HTTPError{StatusCode: 500}} | ||
barErr := Error{Name: "bar", Err: download.HTTPError{StatusCode: 400}} | ||
|
||
errs := Errors{fooErr, barErr} | ||
|
||
// unwrap first bundle.Error | ||
var bundleError Error | ||
if !errors.As(errs, &bundleError) { | ||
t.Fatal("failed to unwrap Error") | ||
} | ||
if bundleError.Error() != fooErr.Error() { | ||
t.Fatalf("expected: %v \ngot: %v", fooErr, bundleError) | ||
} | ||
|
||
// unwrap first HTTPError | ||
var httpError download.HTTPError | ||
if !errors.As(errs, &httpError) { | ||
t.Fatal("failed to unwrap Error") | ||
} | ||
if httpError.Error() != fooErr.Err.Error() { | ||
t.Fatalf("expected: %v \ngot: %v", fooErr.Err, httpError) | ||
} | ||
|
||
// unwrap HTTPError from bundle.Error | ||
if !errors.As(bundleError, &httpError) { | ||
t.Fatal("failed to unwrap HTTPError") | ||
} | ||
if httpError.Error() != fooErr.Err.Error() { | ||
t.Fatalf("expected: %v \nbgot: %v", fooErr.Err, httpError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters