I'm using https://github.com/julienschmidt/httprouter in my Go project.
I asked this question a while back which was solved by @icza: httprouter configuring NotFound but now, starting a new project and using very similar code I appear to be getting errors in the console.
Trying to configure custom handlers NotFound
and MethodNotAllowed
I'm using:
router.NotFound = customNotFound
router.MethodNotAllowed = customMethodNotAllowed
produces:
cannot use customNotFound (type func(http.ResponseWriter, *http.Request)) as type http.Handler in assignment:
func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method)
cannot use customMethodNotAllowed (type func(http.ResponseWriter, *http.Request)) as type http.Handler in assignment:
func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method)
My functions look like this:
func customNotFound(w http.ResponseWriter, r *http.Request) {
core.WriteError(w, "PAGE_NOT_FOUND", "Requested resource could not be found")
return
}
func customMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
core.WriteError(w, "METHOD_NOT_PERMITTED", "Request method not supported by that resource")
return
}
Has there been some breaking changes in this package in the last couple of months as I can't work out why I'm getting the error in one project and not in the other?
Since commit 70708e4600 in httprouter the router.NotFound
is no longer a http.HandlerFunc
but a http.Handler
. So you will have to adapt your functions with via http://golang.org/pkg/net/http/#HandlerFunc if you use a recent commit of httprouter.
The following should work (untested):
router.NotFound = http.HandlerFunc(customNotFound)