Inversed logic of if else statement to reduce nesting by 1.

This commit is contained in:
Alexander Andreev 2023-08-12 19:37:51 +04:00
parent a2cb6182e8
commit 92692454da
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -157,7 +157,12 @@ func New() *Router {
} }
func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if tree, ok := rr.tree[r.Method]; ok { tree, ok := rr.tree[r.Method]
if !ok {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
path, err := newPath(r.URL.Path) path, err := newPath(r.URL.Path)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable) http.Error(w, err.Error(), http.StatusNotAcceptable)
@ -176,9 +181,6 @@ func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", http.StatusNotFound) http.Error(w, "Not Found", http.StatusNotFound)
} }
} }
} else {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
} }
// Handler registers a handler for provided pattern for a given HTTP method. // Handler registers a handler for provided pattern for a given HTTP method.