Skip to content

Commit 8abc058

Browse files
author
Jamie Tanna
committed
refactor: flip error checking to remove nested conditionals
1 parent 8d9c8bf commit 8abc058

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

oapi_validate.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ func OapiRequestValidatorWithOptions(spec *openapi3.T, options *Options) func(ne
6262

6363
return func(next http.Handler) http.Handler {
6464
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65-
6665
// validate request
67-
if statusCode, err := validateRequest(r, router, options); err != nil {
68-
if options != nil && options.ErrorHandler != nil {
69-
options.ErrorHandler(w, err.Error(), statusCode)
70-
} else {
71-
http.Error(w, err.Error(), statusCode)
72-
}
66+
statusCode, err := validateRequest(r, router, options)
67+
if err == nil {
68+
// serve
69+
next.ServeHTTP(w, r)
7370
return
7471
}
7572

76-
// serve
77-
next.ServeHTTP(w, r)
73+
if options != nil && options.ErrorHandler != nil {
74+
options.ErrorHandler(w, err.Error(), statusCode)
75+
} else {
76+
http.Error(w, err.Error(), statusCode)
77+
}
7878
})
7979
}
8080

0 commit comments

Comments
 (0)