Skip to content

Commit d12c756

Browse files
authored
openapi3filter: introduce func ConvertErrors(err error) error (#783)
1 parent cc09e84 commit d12c756

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

.github/docs/openapi3filter.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var ErrAuthenticationServiceMissing = errors.New("missing AuthenticationFunc")
44
var ErrInvalidEmptyValue = errors.New("empty value is not allowed")
55
var ErrInvalidRequired = errors.New("value is required but missing")
66
var JSONPrefixes = []string{ ... }
7+
func ConvertErrors(err error) error
78
func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
89
func FileBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, ...) (interface{}, error)
910
func NoopAuthenticationFunc(context.Context, *AuthenticationInput) error

openapi3filter/validation_error_encoder.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ type ValidationErrorEncoder struct {
1717

1818
// Encode implements the ErrorEncoder interface for encoding ValidationErrors
1919
func (enc *ValidationErrorEncoder) Encode(ctx context.Context, err error, w http.ResponseWriter) {
20+
enc.Encoder(ctx, ConvertErrors(err), w)
21+
}
22+
23+
// ConvertErrors converts all errors to the appropriate error format.
24+
func ConvertErrors(err error) error {
2025
if e, ok := err.(*routers.RouteError); ok {
21-
cErr := convertRouteError(e)
22-
enc.Encoder(ctx, cErr, w)
23-
return
26+
return convertRouteError(e)
2427
}
2528

2629
e, ok := err.(*RequestError)
2730
if !ok {
28-
enc.Encoder(ctx, err, w)
29-
return
31+
return err
3032
}
3133

3234
var cErr *ValidationError
@@ -43,10 +45,9 @@ func (enc *ValidationErrorEncoder) Encode(ctx context.Context, err error, w http
4345
}
4446

4547
if cErr != nil {
46-
enc.Encoder(ctx, cErr, w)
47-
return
48+
return cErr
4849
}
49-
enc.Encoder(ctx, err, w)
50+
return err
5051
}
5152

5253
func convertRouteError(e *routers.RouteError) *ValidationError {

0 commit comments

Comments
 (0)