From 90932f1c6c4a155863a7747529e5133c072efc74 Mon Sep 17 00:00:00 2001 From: Kurtis Nusbaum Date: Fri, 6 Apr 2018 15:46:17 -0700 Subject: [PATCH] add ability to indicate bad args --- connector.go | 24 ++++++++++++++++++++++++ connector_test.go | 16 ++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 connector_test.go diff --git a/connector.go b/connector.go index b314fef8..bb3795d8 100644 --- a/connector.go +++ b/connector.go @@ -28,6 +28,30 @@ import ( "github.com/pkg/errors" ) +// ErrBadArgs allows connectors to indicate to their upstreams +// that something about the args they recieved for a particular operation +// are not valid. +type ErrBadArgs struct { + msg string +} + +func NewErrBadArgs(msg string) error { + return &ErrBadArgs{ + msg: msg, + } +} + +// Error returns the error message for an ErrBadArgs error. +func (e *ErrBadArgs) Error() string { + return e.msg +} + +// ErrorIsBadArgs checks if the error is an "ErrBadArgs". +func ErrorIsBadArgs(err error) bool { + _, ok := errors.Cause(err).(*ErrBadArgs) + return ok +} + //go:generate stringer -type=Operator // Operator defines an operator against some data for range scans diff --git a/connector_test.go b/connector_test.go new file mode 100644 index 00000000..87bc66ed --- /dev/null +++ b/connector_test.go @@ -0,0 +1,16 @@ +package dosa + +import ( + "testing" + + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" +) + +func TestErrorIsBadArgs(t *testing.T) { + badArgsErr := NewErrBadArgs("you can't use that!") + assert.True(t, ErrorIsBadArgs(badArgsErr)) + + regErr := errors.New("hello") + assert.False(t, ErrorIsBadArgs(regErr)) +}