Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mux errors #495

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion mux/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func NewRouter() *Router {
return router
}

// SetErrorHandler sets a custom error handler for the default mux handler set in the constructor.
func (r *Router) SetErrorHandler(h func(error)) {
r.m.Lock()
defer r.m.Unlock()
r.errors = h
}

// Does path match pattern?
func pathMatch(pattern Route, path string) bool {
return pattern.regexMatcher.regexp.MatchString(path)
Expand Down Expand Up @@ -148,9 +155,11 @@ func (r *Router) DefaultHandle(handler Handler) {
}

// HandleFunc adds a handler function to the Router for pattern.
// This function will panic if the pattern parameter is invalid. If the APP provides 'user defined patterns' better
// use Handle(), which will return an error.
func (r *Router) HandleFunc(pattern string, handler func(w ResponseWriter, r *Message)) {
if err := r.Handle(pattern, HandlerFunc(handler)); err != nil {
r.errors(fmt.Errorf("cannot handle pattern(%v): %w", pattern, err))
panic(fmt.Errorf("cannot handle pattern(%v): %w", pattern, err))
}
}

Expand Down