Skip to content

Commit

Permalink
added Unwrap() []error to CompositeError
Browse files Browse the repository at this point in the history
This allows to use errors.Is() or As() with CompositeError

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
  • Loading branch information
fredbi committed Dec 13, 2023
1 parent a29a5f0 commit d08c1d5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-openapi/errors

require github.com/stretchr/testify v1.6.1
require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -10,12 +9,10 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 5 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (c *CompositeError) Error() string {
return c.message
}

func (c *CompositeError) Unwrap() []error {
return c.Errors
}

// MarshalJSON implements the JSON encoding interface
func (c CompositeError) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
Expand All @@ -133,7 +137,7 @@ func (c CompositeError) MarshalJSON() ([]byte, error) {
func CompositeValidationError(errors ...error) *CompositeError {
return &CompositeError{
code: CompositeErrorCode,
Errors: append([]error{}, errors...),
Errors: append(make([]error, 0, len(errors)), errors...),
message: "validation failure list",
}
}
Expand Down
22 changes: 21 additions & 1 deletion schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,30 @@ func TestSchemaErrors(t *testing.T) {
assert.EqualValues(t, CompositeErrorCode, err.Code())
assert.Equal(t, "validation failure list", err.Error())

err = CompositeValidationError(fmt.Errorf("first error"), fmt.Errorf("second error"))
testErr1 := fmt.Errorf("first error")
testErr2 := fmt.Errorf("second error")
err = CompositeValidationError(testErr1, testErr2)
require.Error(t, err)
assert.EqualValues(t, CompositeErrorCode, err.Code())
assert.Equal(t, "validation failure list:\nfirst error\nsecond error", err.Error())

require.ErrorIs(t, err, testErr1)
require.ErrorIs(t, err, testErr2)
})

t.Run("should set validation name in CompositeValidation error", func(t *testing.T) {
err := CompositeValidationError(
InvalidContentType("text/html", []string{"application/json"}),
CompositeValidationError(
InvalidTypeName("y"),
),
)
_ = err.ValidateName("new-name")
const expectedMessage = `validation failure list:
new-name.unsupported media type "text/html", only [application/json] are allowed
validation failure list:
new-namey is an invalid type name`
assert.Equal(t, expectedMessage, err.Error())
})

t.Run("with PropertyNotAllowed", func(t *testing.T) {
Expand Down

0 comments on commit d08c1d5

Please sign in to comment.