-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return NotFounder error, if ID attribute is not present on plugin
- Loading branch information
Showing
3 changed files
with
160 additions
and
1 deletion.
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,66 @@ | ||
package ast | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// NotFounder interface is implemented by errors, that indicate that a record | ||
// is not found. | ||
type NotFounder interface { | ||
Error() string | ||
NotFound() | ||
} | ||
|
||
type notFoundError struct { | ||
err error | ||
} | ||
|
||
// NewNotFoundError wraps an error as a not found error. | ||
func NewNotFoundError(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return notFoundError{ | ||
err: err, | ||
} | ||
} | ||
|
||
// NotFoundErrorf formats according to a format specifier and returns the string | ||
// as a value that satisfies NotFounder. | ||
func NotFoundErrorf(format string, a ...interface{}) error { | ||
return NewNotFoundError(fmt.Errorf(format, a...)) | ||
} | ||
|
||
// NotFound implements the NotFounder interface to indicate, that the error is | ||
// of type not found. | ||
func (e notFoundError) NotFound() {} | ||
|
||
func (e notFoundError) Error() string { | ||
return "not found: " + e.err.Error() | ||
} | ||
|
||
// Format implements the fmt.Formatter interface to print the error differently | ||
// depending on the format verb. | ||
func (e notFoundError) Format(s fmt.State, verb rune) { | ||
switch verb { | ||
case 'v': | ||
if s.Flag('+') { | ||
fmt.Fprintf(s, "not found: %+v", e.err) | ||
return | ||
} | ||
fallthrough | ||
case 's': | ||
fmt.Fprint(s, e.Error()) | ||
case 'q': | ||
fmt.Fprintf(s, "%q", e.Error()) | ||
} | ||
} | ||
|
||
// IsNotFoundError returns true, if the provided error implements the NotFound | ||
// interface. | ||
func IsNotFoundError(e error) bool { | ||
var notFoundErr NotFounder | ||
return errors.As(e, ¬FoundErr) | ||
} |
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,91 @@ | ||
package ast_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/breml/logstash-config/ast" | ||
) | ||
|
||
func TestNotFoundError(t *testing.T) { | ||
cases := []struct { | ||
formatVerb string | ||
|
||
wantErrorString string | ||
}{ | ||
{ | ||
formatVerb: "%s", | ||
|
||
wantErrorString: "not found: error", | ||
}, | ||
{ | ||
formatVerb: "%v", | ||
|
||
wantErrorString: "not found: error", | ||
}, | ||
{ | ||
formatVerb: "%+v", | ||
|
||
wantErrorString: "not found: error", | ||
}, | ||
{ | ||
formatVerb: "%q", | ||
|
||
wantErrorString: `"not found: error"`, | ||
}, | ||
} | ||
|
||
for _, test := range cases { | ||
t.Run(test.formatVerb, func(t *testing.T) { | ||
err := ast.NotFoundErrorf("%s", "error") | ||
|
||
if err == nil { | ||
t.Fatal("Expect an error, but got none.") | ||
} | ||
|
||
if test.wantErrorString != fmt.Sprintf(test.formatVerb, err) { | ||
t.Errorf("Expect error to print %q with verb %q, but got: %q", test.wantErrorString, test.formatVerb, fmt.Sprintf(test.formatVerb, err)) | ||
} | ||
|
||
if !ast.IsNotFoundError(err) { | ||
t.Fatalf("Expect err %v to implement NotFounder interface, but it does not.", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNotFoundErrorNotFound(t *testing.T) { | ||
ast.NewNotFoundError(errors.New("error")).(ast.NotFounder).NotFound() | ||
} | ||
|
||
func TestIsNotFoundError(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
err error | ||
|
||
want bool | ||
}{ | ||
{ | ||
name: "nil", | ||
}, | ||
{ | ||
name: "nil not founder", | ||
err: ast.NewNotFoundError(nil), | ||
}, | ||
{ | ||
name: "error", | ||
err: ast.NewNotFoundError(errors.New("error")), | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, test := range cases { | ||
t.Run(test.name, func(t *testing.T) { | ||
got := ast.IsNotFoundError(test.err) | ||
if test.want != got { | ||
t.Fatalf("Expectation (%v) not met: %v", test.want, got) | ||
} | ||
}) | ||
} | ||
} |