英文:
httprouter setting custom NotFound
问题
我在我的Go项目中使用https://github.com/julienschmidt/httprouter。
我之前提出了一个问题,由@icza解决:https://stackoverflow.com/questions/30500322/httprouter-configuring-notfound,但是现在,我正在启动一个新项目,并且使用非常相似的代码,但是在控制台中出现错误。
我尝试配置自定义处理程序NotFound
和MethodNotAllowed
,我使用以下代码:
router.NotFound = customNotFound
router.MethodNotAllowed = customMethodNotAllowed
产生以下错误:
无法将customNotFound(类型为func(http.ResponseWriter,*http.Request))用作分配中的类型http.Handler:
func(http.ResponseWriter,*http.Request)不实现http.Handler(缺少ServeHTTP方法)
无法将customMethodNotAllowed(类型为func(http.ResponseWriter,*http.Request))用作分配中的类型http.Handler:
func(http.ResponseWriter,*http.Request)不实现http.Handler(缺少ServeHTTP方法)
我的函数如下所示:
func customNotFound(w http.ResponseWriter, r *http.Request) {
core.WriteError(w, "PAGE_NOT_FOUND", "请求的资源未找到")
return
}
func customMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
core.WriteError(w, "METHOD_NOT_PERMITTED", "请求方法不受该资源支持")
return
}
在过去几个月中,这个包是否有一些重大变化?我无法理解为什么一个项目中会出现错误,而另一个项目中却没有出现错误。
英文:
I'm using https://github.com/julienschmidt/httprouter in my Go project.
I asked this question a while back which was solved by @icza: https://stackoverflow.com/questions/30500322/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?
答案1
得分: 0
自从httprouter的提交70708e4600之后,router.NotFound
不再是http.HandlerFunc
,而是http.Handler
。因此,如果你使用httprouter的最新提交版本,你需要通过http://golang.org/pkg/net/http/#HandlerFunc来调整你的函数。
以下是可能适用的代码(未经测试):
router.NotFound = http.HandlerFunc(customNotFound)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论