Skip to content

Commit

Permalink
Merge pull request #93 from alecsammon/owl_error
Browse files Browse the repository at this point in the history
Expose core.ErrUnsupportedType so clients can use errors.Is()
  • Loading branch information
ggicci authored Jan 28, 2024
2 parents 5dca4c8 + e6be2b3 commit af34b7c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
7 changes: 3 additions & 4 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/ggicci/httpin/internal"
"github.com/ggicci/owl"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func (p *Place) FromString(value string) error {
func TestNew_WithNonStructType(t *testing.T) {
co, err := New(string("hello"))
assert.Nil(t, co)
assert.ErrorIs(t, err, owl.ErrUnsupportedType)
assert.ErrorIs(t, err, ErrUnsupportedType)
}

func TestNew_ErrUnregisteredDirective(t *testing.T) {
Expand Down Expand Up @@ -226,7 +225,7 @@ func TestCore_Decode_ErrUnsupporetedType(t *testing.T) {
co, err := New(Cursor{})
assert.NoError(t, err)
got, err := co.Decode(r)
assert.ErrorIs(t, err, internal.ErrUnsupportedType)
assert.ErrorIs(t, err, ErrUnsupportedType)
assert.ErrorContains(t, err, "ObjectID")
assert.Nil(t, got)
}()
Expand All @@ -244,7 +243,7 @@ func TestCore_Decode_ErrUnsupporetedType(t *testing.T) {
co, err := New(Payload{})
assert.NoError(t, err)
got, err := co.Decode(r)
assert.ErrorIs(t, err, internal.ErrUnsupportedType)
assert.ErrorIs(t, err, ErrUnsupportedType)
assert.ErrorContains(t, err, "ObjectID")
assert.Nil(t, got)
}()
Expand Down
3 changes: 1 addition & 2 deletions core/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/url"
"testing"

"github.com/ggicci/httpin/internal"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -106,7 +105,7 @@ func TestDirectiveQuery_NewRequest_ErrUnsupportedType(t *testing.T) {
co, err := New(SearchQuery{})
assert.NoError(t, err)
_, err = co.NewRequest("GET", "/pets", &SearchQuery{})
assert.ErrorIs(t, err, internal.ErrUnsupportedType)
assert.ErrorIs(t, err, ErrUnsupportedType)
}

// See hybridcoder_test.go for more details.
Expand Down
3 changes: 3 additions & 0 deletions core/stringable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"time"

"github.com/ggicci/httpin/internal"
"github.com/ggicci/owl"
)

type Stringable = internal.Stringable

var ErrUnsupportedType = owl.ErrUnsupportedType

func NewStringable(rv reflect.Value, adapt AnyStringableAdaptor) (stringable Stringable, err error) {
if IsPatchField(rv.Type()) {
stringable, err = NewStringablePatchFieldWrapper(rv, adapt)
Expand Down
2 changes: 1 addition & 1 deletion core/stringable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func testAssignString(t *testing.T, rv reflect.Value, value string) {

func testNewStringableErrUnsupported(t *testing.T, rv reflect.Value) {
s, err := NewStringable(rv, nil)
assert.ErrorIs(t, err, internal.ErrUnsupportedType)
assert.ErrorIs(t, err, ErrUnsupportedType)
assert.Nil(t, s)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/stringable.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"strconv"
"strings"
"time"
"github.com/ggicci/owl"
)

var (
ErrUnsupportedType = errors.New("unsupported type")
ErrTypeMismatch = errors.New("type mismatch")
ErrUnsupportedType = owl.ErrUnsupportedType

builtinStringableAdaptors = make(map[reflect.Type]AnyStringableAdaptor)
)
Expand Down
3 changes: 2 additions & 1 deletion internal/stringable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/ggicci/owl"
)

func TestNewStringable_string(t *testing.T) {
Expand Down Expand Up @@ -156,7 +157,7 @@ func TestNewStringable_ErrUnsupportedType(t *testing.T) {
})
rvStructPointer := reflect.ValueOf(&s)
sv, err := NewStringable(rvStructPointer)
assert.ErrorIs(t, err, ErrUnsupportedType)
assert.ErrorIs(t, err, owl.ErrUnsupportedType)
assert.Nil(t, sv)
}

Expand Down

0 comments on commit af34b7c

Please sign in to comment.