httprouter配置NotFound

huangapple go评论76阅读模式
英文:

httprouter configuring NotFound

问题

我正在使用httprouter来构建一个API,并且我正在尝试解决如何处理404错误的问题。在文档中确实提到可以手动处理404错误,但我不太清楚如何编写自己的自定义处理程序

我在其他路由之后尝试了以下代码...

router.NotFound(pageNotFound)

但是我得到了错误信息not enough arguments in call to router.NotFound

如果有人能指点我正确的方向,那就太好了。

英文:

I'm using httprouter for an API and I'm trying to work out how to handle 404s. It does say in the docs that 404's can be handled manually but I don't really have an idea how to write my own custom handler.

I've tried the following after my other routes...

router.NotFound(pageNotFound)

But I get the error not enough arguments in call to router.NotFound.

If anyone could point me in the right direction it would be great.

答案1

得分: 9

httprouter.Router 类型是一个 struct,它有一个字段:

NotFound http.Handler

所以 NotFound 的类型是 http.Handler,它是一个接口类型,只有一个方法:

ServeHTTP(ResponseWriter, *Request)

如果你想要自定义自己的 "Not Found" 处理程序,你需要设置一个实现了这个接口的值。

最简单的方法是定义一个具有以下签名的函数:

func(http.ResponseWriter, *http.Request)

然后使用 http.HandlerFunc() 辅助函数将其 "转换" 为实现 http.Handler 接口的值,该接口的 ServeHTTP() 方法只是调用具有上述签名的函数。

例如:

func MyNotFound(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    w.WriteHeader(http.StatusNotFound) // StatusNotFound = 404
    w.Write([]byte("My own Not Found handler."))
    w.Write([]byte(" The page you requested could not be found."))
}

var router *httprouter.Router = ... // 你的路由器值
router.NotFound = http.HandlerFunc(MyNotFound)

这个 NotFound 处理程序将由 httprouter 包调用。如果你想要在其他处理程序中手动调用它,你需要传递一个 ResponseWriter 和一个 *Request,类似这样:

func ResourceHandler(w http.ResponseWriter, r *http.Request) {
    exists := ... // 查看请求的资源是否有效和可用
    if !exists {
        MyNotFound(w, r) // 传递 ResponseWriter 和 Request
        // 或者通过 Router:
        // router.NotFound(w, r)
        return
    }

    // 资源存在,提供服务
    // ...
}
英文:

The type httprouter.Router is a struct which has a field:

NotFound http.Handler

So type of NotFound is http.Handler, an interface type which has a single method:

ServeHTTP(ResponseWriter, *Request)

If you want your own custom "Not Found" handler, you have to set a value that implements this interface.

The easiest way is to define a function with signature:

func(http.ResponseWriter, *http.Request)

And use the http.HandlerFunc() helper function to "convert" it to a value that implements the http.Handler interface, whose ServeHTTP() method simply calls the function with the above signature.

For example:

func MyNotFound(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    w.WriteHeader(http.StatusNotFound) // StatusNotFound = 404
    w.Write([]byte("My own Not Found handler."))
    w.Write([]byte(" The page you requested could not be found."))
}

var router *httprouter.Router = ... // Your router value
router.NotFound = http.HandlerFunc(MyNotFound)

This NotFound handler will be invoked by the httprouter package. If you would want to call it manually from one of your other handlers, you would have to pass a ResponseWriter and a *Request to it, something like this:

func ResourceHandler(w http.ResponseWriter, r *http.Request) {
    exists := ... // Find out if requested resource is valid and available
    if !exists {
        MyNotFound(w, r) // Pass ResponseWriter and Request
        // Or via the Router:
        // router.NotFound(w, r)
        return
    }

    // Resource exists, serve it
    // ...
}

答案2

得分: 1

NotFound是一个类型为http.HandlerFunc的函数指针。你需要定义一个具有Func(w ResponseWriter, r *Request)签名的函数,并将其赋值给NotFound(router.NotFound = Func)。

英文:

NotFound is a function pointer of type http.HandlerFunc. You will have to put a function with the signature Func(w ResponseWriter, r *Request) somewhere and assign it to NotFound (router.NotFound = Func).

huangapple
  • 本文由 发表于 2015年5月28日 15:47:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/30500322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定