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

fix panic when both handler and middlewares are nil #2500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion echo.go
Expand Up @@ -575,7 +575,9 @@ func (e *Echo) File(path, file string, m ...MiddlewareFunc) *Route {

func (e *Echo) add(host, method, path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *Route {
router := e.findRouter(host)
//FIXME: when handler+middleware are both nil ... make it behave like handler removal
if !e.hasValidHandlerAndMiddleware(handler, middlewares...) {
return nil
}
name := handlerName(handler)
route := router.add(method, path, name, func(c Context) error {
h := applyMiddleware(handler, middlewares...)
Expand Down Expand Up @@ -972,6 +974,18 @@ func handlerName(h HandlerFunc) string {
return t.String()
}

func (e *Echo) hasValidHandlerAndMiddleware(handler HandlerFunc, middlewares ...MiddlewareFunc) bool {
if handler == nil {
for _, middleware := range middlewares {
if middleware != nil {
return true
}
}
return false
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean when both of them were nil ?

Suggested change
return false
panic("can not add route with nil handler and middlewares")

}
return true
}

// // PathUnescape is wraps `url.PathUnescape`
// func PathUnescape(s string) (string, error) {
// return url.PathUnescape(s)
Expand Down