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

NotFoundHandler: panic: echo: unknown handler #53

Closed
ivoviz opened this issue May 12, 2015 · 6 comments
Closed

NotFoundHandler: panic: echo: unknown handler #53

ivoviz opened this issue May 12, 2015 · 6 comments
Assignees
Labels

Comments

@ivoviz
Copy link

ivoviz commented May 12, 2015

The following code fails with panic: echo: unknown handler line 16: e.NotFoundHandler(index)

package main

import (
    "net/http"
    "github.com/labstack/echo"
)

func index(c *echo.Context) {
    c.HTML(http.StatusOK, "<html></html>")
}

func main() {
    e := echo.New()
    e.NotFoundHandler(index)
    e.Run(":1234")
}

I have an older version of echo (54d2f72) and with that the code above works just fine. Could this be a bug or am I missing something?

@vishr
Copy link
Member

vishr commented May 12, 2015

It's not a bug, support for fun(*echo.Context) was dropped lately to encourage use of echo.HandlerFunc which focuses on centralized HTTP error handling using echo.HTTPErrorHandler

@vishr vishr added the question label May 12, 2015
@vishr vishr self-assigned this May 12, 2015
@ivoviz
Copy link
Author

ivoviz commented May 12, 2015

Even if I use echo.HandlerFunc it throws the exact same error for me.

@vishr
Copy link
Member

vishr commented May 12, 2015

If you get the latest, it should work. echo.HandlerFunc was refactored in https://github.com/labstack/echo/releases/tag/v0.0.12

@ivoviz
Copy link
Author

ivoviz commented May 12, 2015

I must be dumb, but even with that release it doesn't work. :)

I've checked echo_test.go and I saw it uses func(c *Context) *HTTPError for NotFoundHandler. Changing the code to

func index(c *echo.Context) *echo.HTTPError {
    return c.HTML(http.StatusOK, "<html></html>")
}

makes it work, but I'm just not sure anymore if this is the right way.

@vishr
Copy link
Member

vishr commented May 12, 2015

This is the right way, by doing so you don't have to deal with errors as it's taken care by echo.HTTPErrorHandler (which can be customized) - all you do is return them as echo.*HTTPError. For concrete examples, you can explore examples directory in the repository.

@ivoviz
Copy link
Author

ivoviz commented May 12, 2015

Okay, thanks!

@ivoviz ivoviz closed this as completed May 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants