Let Handler() return an error instead of panicking. Also removed checks for / and * since they are checked in newPath().

This commit is contained in:
Alexander Andreev 2023-05-28 01:24:18 +04:00
parent 99a7cebd0a
commit a0b80ced85
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -157,22 +157,21 @@ func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// Handler registers a handler for provided pattern for a given HTTP method. // Handler registers a handler for provided pattern for a given HTTP method.
func (rr *Router) Handler(method, pattern string, handler http.HandlerFunc) { func (rr *Router) Handler(method, pattern string, handler http.HandlerFunc) error {
if pattern[0] != '/' { path, err := newPath(pattern)
panic("first element of path should be a slash (/) symbol") if err != nil {
} return err
if strings.Count(pattern, "*") > 1 {
panic("there can be only one wildcard (*) symbol in path")
} }
if rr.tree[method] == nil { if rr.tree[method] == nil {
rr.tree[method] = &node{endpoint: "/"} rr.tree[method] = &node{endpoint: "/"}
} }
path, _ := newPath(pattern) if err := rr.tree[method].add(path, handler); err != nil {
return err
}
rr.tree[method].add(path, 0, handler) return nil
} }
// ServeStatic serves a given file system. // ServeStatic serves a given file system.