Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/errors)](https://goreportcard.com/report/github.com/go-openapi/errors)

Shared errors and error interface used throughout the various libraries found in the go-openapi toolkit.

## Licensing

This library ships under the [SPDX-License-Identifier: Apache-2.0](./LICENSE).
8 changes: 4 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (a *apiError) Code() int32 {

// MarshalJSON implements the JSON encoding interface
func (a apiError) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
return json.Marshal(map[string]any{
"code": a.code,
"message": a.message,
})
}

// New creates a new API error with a code and a message
func New(code int32, message string, args ...interface{}) Error {
func New(code int32, message string, args ...any) Error {
if len(args) > 0 {
return &apiError{
code: code,
Expand All @@ -56,7 +56,7 @@ func New(code int32, message string, args ...interface{}) Error {
}

// NotFound creates a new not found error
func NotFound(message string, args ...interface{}) Error {
func NotFound(message string, args ...any) Error {
if message == "" {
message = "Not found"
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (m *MethodNotAllowedError) Code() int32 {

// MarshalJSON implements the JSON encoding interface
func (m MethodNotAllowedError) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
return json.Marshal(map[string]any{
"code": m.code,
"message": m.message,
"allowed": m.Allowed,
Expand Down
4 changes: 2 additions & 2 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

type customError struct {
Expand Down
2 changes: 1 addition & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package errors
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/go-openapi/testify/v2/assert"
)

func TestUnauthenticated(t *testing.T) {
Expand Down
11 changes: 1 addition & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
module github.com/go-openapi/errors

require github.com/stretchr/testify v1.11.1

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require github.com/go-openapi/testify/v2 v2.0.2

go 1.24.0
20 changes: 2 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,2 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
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/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
github.com/go-openapi/testify/v2 v2.0.2 h1:X999g3jeLcoY8qctY/c/Z8iBHTbwLz7R2WXd6Ub6wls=
github.com/go-openapi/testify/v2 v2.0.2/go.mod h1:HCPmvFFnheKK2BuwSA0TbbdxJ3I16pjwMkYkP4Ywn54=
10 changes: 5 additions & 5 deletions headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type Validation struct { //nolint: errname
code int32
Name string
In string
Value interface{}
Value any
message string
Values []interface{}
Values []any
}

func (e *Validation) Error() string {
Expand All @@ -30,7 +30,7 @@ func (e *Validation) Code() int32 {

// MarshalJSON implements the JSON encoding interface
func (e Validation) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
return json.Marshal(map[string]any{
"code": e.code,
"message": e.message,
"in": e.In,
Expand Down Expand Up @@ -61,7 +61,7 @@ const (

// InvalidContentType error for an invalid content type
func InvalidContentType(value string, allowed []string) *Validation {
values := make([]interface{}, 0, len(allowed))
values := make([]any, 0, len(allowed))
for _, v := range allowed {
values = append(values, v)
}
Expand All @@ -77,7 +77,7 @@ func InvalidContentType(value string, allowed []string) *Validation {

// InvalidResponseFormat error for an unacceptable response format request
func InvalidResponseFormat(value string, allowed []string) *Validation {
values := make([]interface{}, 0, len(allowed))
values := make([]any, 0, len(allowed))
for _, v := range allowed {
values = append(values, v)
}
Expand Down
2 changes: 1 addition & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package errors
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/go-openapi/testify/v2/assert"
)

func TestAPIVerificationFailed(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (e ParseError) MarshalJSON() ([]byte, error) {
if e.Reason != nil {
reason = e.Reason.Error()
}
return json.Marshal(map[string]interface{}{
return json.Marshal(map[string]any{
"code": e.code,
"message": e.message,
"in": e.In,
Expand Down
2 changes: 1 addition & 1 deletion parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/go-openapi/testify/v2/assert"
)

func TestParseError(t *testing.T) {
Expand Down
36 changes: 18 additions & 18 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *CompositeError) Unwrap() []error {

// MarshalJSON implements the JSON encoding interface
func (c CompositeError) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
return json.Marshal(map[string]any{
"code": c.code,
"message": c.message,
"errors": c.Errors,
Expand Down Expand Up @@ -243,7 +243,7 @@ func InvalidTypeName(typeName string) *Validation {
}

// InvalidType creates an error for when the type is invalid
func InvalidType(name, in, typeName string, value interface{}) *Validation {
func InvalidType(name, in, typeName string, value any) *Validation {
var message string

if in != "" {
Expand Down Expand Up @@ -291,7 +291,7 @@ func DuplicateItems(name, in string) *Validation {
}

// TooManyItems error for when an array contains too many items
func TooManyItems(name, in string, maximum int64, value interface{}) *Validation {
func TooManyItems(name, in string, maximum int64, value any) *Validation {
msg := fmt.Sprintf(maximumItemsFail, name, in, maximum)
if in == "" {
msg = fmt.Sprintf(maximumItemsFailNoIn, name, maximum)
Expand All @@ -307,7 +307,7 @@ func TooManyItems(name, in string, maximum int64, value interface{}) *Validation
}

// TooFewItems error for when an array contains too few items
func TooFewItems(name, in string, minimum int64, value interface{}) *Validation {
func TooFewItems(name, in string, minimum int64, value any) *Validation {
msg := fmt.Sprintf(minItemsFail, name, in, minimum)
if in == "" {
msg = fmt.Sprintf(minItemsFailNoIn, name, minimum)
Expand All @@ -322,7 +322,7 @@ func TooFewItems(name, in string, minimum int64, value interface{}) *Validation
}

// ExceedsMaximumInt error for when maximumimum validation fails
func ExceedsMaximumInt(name, in string, maximum int64, exclusive bool, value interface{}) *Validation {
func ExceedsMaximumInt(name, in string, maximum int64, exclusive bool, value any) *Validation {
var message string
if in == "" {
m := maximumIncFailNoIn
Expand All @@ -347,7 +347,7 @@ func ExceedsMaximumInt(name, in string, maximum int64, exclusive bool, value int
}

// ExceedsMaximumUint error for when maximumimum validation fails
func ExceedsMaximumUint(name, in string, maximum uint64, exclusive bool, value interface{}) *Validation {
func ExceedsMaximumUint(name, in string, maximum uint64, exclusive bool, value any) *Validation {
var message string
if in == "" {
m := maximumIncFailNoIn
Expand All @@ -372,7 +372,7 @@ func ExceedsMaximumUint(name, in string, maximum uint64, exclusive bool, value i
}

// ExceedsMaximum error for when maximumimum validation fails
func ExceedsMaximum(name, in string, maximum float64, exclusive bool, value interface{}) *Validation {
func ExceedsMaximum(name, in string, maximum float64, exclusive bool, value any) *Validation {
var message string
if in == "" {
m := maximumIncFailNoIn
Expand All @@ -397,7 +397,7 @@ func ExceedsMaximum(name, in string, maximum float64, exclusive bool, value inte
}

// ExceedsMinimumInt error for when minimum validation fails
func ExceedsMinimumInt(name, in string, minimum int64, exclusive bool, value interface{}) *Validation {
func ExceedsMinimumInt(name, in string, minimum int64, exclusive bool, value any) *Validation {
var message string
if in == "" {
m := minIncFailNoIn
Expand All @@ -422,7 +422,7 @@ func ExceedsMinimumInt(name, in string, minimum int64, exclusive bool, value int
}

// ExceedsMinimumUint error for when minimum validation fails
func ExceedsMinimumUint(name, in string, minimum uint64, exclusive bool, value interface{}) *Validation {
func ExceedsMinimumUint(name, in string, minimum uint64, exclusive bool, value any) *Validation {
var message string
if in == "" {
m := minIncFailNoIn
Expand All @@ -447,7 +447,7 @@ func ExceedsMinimumUint(name, in string, minimum uint64, exclusive bool, value i
}

// ExceedsMinimum error for when minimum validation fails
func ExceedsMinimum(name, in string, minimum float64, exclusive bool, value interface{}) *Validation {
func ExceedsMinimum(name, in string, minimum float64, exclusive bool, value any) *Validation {
var message string
if in == "" {
m := minIncFailNoIn
Expand All @@ -472,7 +472,7 @@ func ExceedsMinimum(name, in string, minimum float64, exclusive bool, value inte
}

// NotMultipleOf error for when multiple of validation fails
func NotMultipleOf(name, in string, multiple, value interface{}) *Validation {
func NotMultipleOf(name, in string, multiple, value any) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(multipleOfFailNoIn, name, multiple)
Expand All @@ -489,7 +489,7 @@ func NotMultipleOf(name, in string, multiple, value interface{}) *Validation {
}

// EnumFail error for when an enum validation fails
func EnumFail(name, in string, value interface{}, values []interface{}) *Validation {
func EnumFail(name, in string, value any, values []any) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(enumFailNoIn, name, values)
Expand All @@ -508,7 +508,7 @@ func EnumFail(name, in string, value interface{}, values []interface{}) *Validat
}

// Required error for when a value is missing
func Required(name, in string, value interface{}) *Validation {
func Required(name, in string, value any) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(requiredFailNoIn, name)
Expand All @@ -525,7 +525,7 @@ func Required(name, in string, value interface{}) *Validation {
}

// ReadOnly error for when a value is present in request
func ReadOnly(name, in string, value interface{}) *Validation {
func ReadOnly(name, in string, value any) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(readOnlyFailNoIn, name)
Expand All @@ -542,7 +542,7 @@ func ReadOnly(name, in string, value interface{}) *Validation {
}

// TooLong error for when a string is too long
func TooLong(name, in string, maximum int64, value interface{}) *Validation {
func TooLong(name, in string, maximum int64, value any) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(tooLongMessageNoIn, name, maximum)
Expand All @@ -559,7 +559,7 @@ func TooLong(name, in string, maximum int64, value interface{}) *Validation {
}

// TooShort error for when a string is too short
func TooShort(name, in string, minimum int64, value interface{}) *Validation {
func TooShort(name, in string, minimum int64, value any) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(tooShortMessageNoIn, name, minimum)
Expand All @@ -578,7 +578,7 @@ func TooShort(name, in string, minimum int64, value interface{}) *Validation {

// FailedPattern error for when a string fails a regex pattern match
// the pattern that is returned is the ECMA syntax version of the pattern not the golang version.
func FailedPattern(name, in, pattern string, value interface{}) *Validation {
func FailedPattern(name, in, pattern string, value any) *Validation {
var msg string
if in == "" {
msg = fmt.Sprintf(patternFailNoIn, name, pattern)
Expand All @@ -597,7 +597,7 @@ func FailedPattern(name, in, pattern string, value interface{}) *Validation {

// MultipleOfMustBePositive error for when a
// multipleOf factor is negative
func MultipleOfMustBePositive(name, in string, factor interface{}) *Validation {
func MultipleOfMustBePositive(name, in string, factor any) *Validation {
return &Validation{
code: MultipleOfMustBePositiveCode,
Name: name,
Expand Down
8 changes: 4 additions & 4 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

//nolint:maintidx
Expand Down Expand Up @@ -265,13 +265,13 @@ func TestSchemaErrors(t *testing.T) {
})

t.Run("with EnumFail", func(t *testing.T) {
err := EnumFail("something", "query", "yada", []interface{}{"hello", "world"})
err := EnumFail("something", "query", "yada", []any{"hello", "world"})
require.Error(t, err)
assert.EqualValues(t, EnumFailCode, err.Code())
assert.Equal(t, "something in query should be one of [hello world]", err.Error())
assert.Equal(t, "yada", err.Value)

err = EnumFail("something", "", "yada", []interface{}{"hello", "world"})
err = EnumFail("something", "", "yada", []any{"hello", "world"})
require.Error(t, err)
assert.EqualValues(t, EnumFailCode, err.Code())
assert.Equal(t, "something should be one of [hello world]", err.Error())
Expand Down
Loading