Skip to content

Commit 83a8756

Browse files
Jamie Tannajamietanna
authored andcommitted
refactor: flip error checking to remove nested conditionals
1 parent 0cb46bb commit 83a8756

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

oapi_validate.go

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

6969
return func(next http.Handler) http.Handler {
7070
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
71-
7271
// validate request
73-
if statusCode, err := validateRequest(r, router, options); err != nil {
74-
if options != nil && options.ErrorHandler != nil {
75-
options.ErrorHandler(w, err.Error(), statusCode)
76-
} else {
77-
http.Error(w, err.Error(), statusCode)
78-
}
72+
statusCode, err := validateRequest(r, router, options)
73+
if err == nil {
74+
// serve
75+
next.ServeHTTP(w, r)
7976
return
8077
}
8178

82-
// serve
83-
next.ServeHTTP(w, r)
79+
if options != nil && options.ErrorHandler != nil {
80+
options.ErrorHandler(w, err.Error(), statusCode)
81+
} else {
82+
http.Error(w, err.Error(), statusCode)
83+
}
8484
})
8585
}
8686

0 commit comments

Comments
 (0)