Skip to content

Commit

Permalink
openapi3filter: introduce func ConvertErrors(err error) error (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
QifanWuCFLT committed Apr 18, 2023
1 parent cc09e84 commit d12c756
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/docs/openapi3filter.txt
Expand Up @@ -4,6 +4,7 @@ var ErrAuthenticationServiceMissing = errors.New("missing AuthenticationFunc")
var ErrInvalidEmptyValue = errors.New("empty value is not allowed")
var ErrInvalidRequired = errors.New("value is required but missing")
var JSONPrefixes = []string{ ... }
func ConvertErrors(err error) error
func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
func FileBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, ...) (interface{}, error)
func NoopAuthenticationFunc(context.Context, *AuthenticationInput) error
Expand Down
17 changes: 9 additions & 8 deletions openapi3filter/validation_error_encoder.go
Expand Up @@ -17,16 +17,18 @@ type ValidationErrorEncoder struct {

// Encode implements the ErrorEncoder interface for encoding ValidationErrors
func (enc *ValidationErrorEncoder) Encode(ctx context.Context, err error, w http.ResponseWriter) {
enc.Encoder(ctx, ConvertErrors(err), w)
}

// ConvertErrors converts all errors to the appropriate error format.
func ConvertErrors(err error) error {
if e, ok := err.(*routers.RouteError); ok {
cErr := convertRouteError(e)
enc.Encoder(ctx, cErr, w)
return
return convertRouteError(e)
}

e, ok := err.(*RequestError)
if !ok {
enc.Encoder(ctx, err, w)
return
return err
}

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

if cErr != nil {
enc.Encoder(ctx, cErr, w)
return
return cErr
}
enc.Encoder(ctx, err, w)
return err
}

func convertRouteError(e *routers.RouteError) *ValidationError {
Expand Down

0 comments on commit d12c756

Please sign in to comment.