httprouter 设置自定义 NotFound

我在 Go 项目中使用https://github.com/julienschmidt/httprouter。


不久前我问了这个问题,@ icza解决了这个问题:httprouter 配置 NotFound但现在,开始一个新项目并使用非常相似的代码,我似乎在控制台中遇到错误。


尝试配置自定义处理程序NotFound,MethodNotAllowed我正在使用:


router.NotFound = customNotFound

router.MethodNotAllowed = customMethodNotAllowed

产生:


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)  

我的功能如下所示:


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

}

在过去的几个月里,这个包是否发生了一些重大变化,因为我不知道为什么我在一个项目中而不是在另一个项目中收到错误?


holdtom
浏览 197回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go