错误处理

在本章中,我们将学习如何从路由处理程序返回错误以及Buffalo如何捕获任何未处理的错误。

从处理程序返回错误

最简单抛出错误的方法是返回一个标准Go错误。这个时候返回的http状态会是500。

func MyHandler(c buffalo.Context) error {
  // Return any go error, this will result in a 500 status code.
  return errors.New("boom!")
}

也可以返回一个空的错误。就是正常返回200状态

func MyHandler(c buffalo.Context) error {
  // HTTP 200
  return nil
}

也可以自动http状态。

func MyHandler(c buffalo.Context) error {
  // Use the Error function on the context.
  // This will result in a status code of 401.
  return c.Error(401, errors.New("Unauthorized!"))
}

默认错误处理(开发)

在“开发”模式中,Buffalo将会生成一些有用的错误页面。

错误处理

如果使用JSON或XML内容类型,则会以正确的类型返回错误

{
  "error": "could not find test/",
  "trace": "could not find test/\ngithub.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo.(*App).fileServer.func1\n\t/home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo/route_mappings.go:97\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:1947\nnet/http.StripPrefix.func1\n\t/usr/local/go/src/net/http/server.go:1986\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:1947\ngithub.com/gobuffalo/gobuffalo/vendor/github.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/gorilla/mux/mux.go:162\ngithub.com/gobuffalo/gobuffalo/vendor/github.com/markbates/refresh/refresh/web.ErrorChecker.func1\n\t/home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/markbates/refresh/refresh/web/web.go:23\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:1947\ngithub.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo.(*App).ServeHTTP\n\t/home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo/server.go:127\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2694\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1830\nruntime.goexit\n\t/usr/local/go/src/runtime/asm_amd64.s:2361",
  "code": 404
}
<response code="404">
  <error>could not find test/</error>
  <trace>could not find test/
github.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo.(*App).fileServer.func1
    /home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo/route_mappings.go:97
net/http.HandlerFunc.ServeHTTP
    /usr/local/go/src/net/http/server.go:1947
net/http.StripPrefix.func1
    /usr/local/go/src/net/http/server.go:1986
net/http.HandlerFunc.ServeHTTP
    /usr/local/go/src/net/http/server.go:1947
github.com/gobuffalo/gobuffalo/vendor/github.com/gorilla/mux.(*Router).ServeHTTP
    /home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/gorilla/mux/mux.go:162
github.com/gobuffalo/gobuffalo/vendor/github.com/markbates/refresh/refresh/web.ErrorChecker.func1
    /home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/markbates/refresh/refresh/web/web.go:23
net/http.HandlerFunc.ServeHTTP
    /usr/local/go/src/net/http/server.go:1947
github.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo.(*App).ServeHTTP
    /home/michalakst/go/src/github.com/gobuffalo/gobuffalo/vendor/github.com/gobuffalo/buffalo/server.go:127
net/http.serverHandler.ServeHTTP
    /usr/local/go/src/net/http/server.go:2694
net/http.(*conn).serve
    /usr/local/go/src/net/http/server.go:1830
runtime.goexit
    /usr/local/go/src/runtime/asm_amd64.s:2361</trace>
</response>

在“生产”模式中,Buffalo不会生成具有开发者样式信息的页面,因为这会给黑客提供宝贵的信息。相反,页面更简单。

自定义错误处理

虽然Buffalo将为您提供开箱即用的错误,但以自定义方式处理错误会很有用。为此,Buffalo允许将HTTP状态代码映射到特定处理程序。这意味着错误可以以自定义方式处理。

app = buffalo.New(buffalo.Options{
  Env: ENV,
})

// We associate the HTTP 422 status to a specific handler.
// All the other status code will still use the default handler provided by Buffalo.
app.ErrorHandlers[422] = func(status int, err error, c buffalo.Context) error {
  res := c.Response()
  res.WriteHeader(422)
  res.Write([]byte(fmt.Sprintf("Oops!! There was an error %s", err.Error())))
  return nil
}

app.GET("/oops", MyHandler)

func MyHandler(c buffalo.Context) error {
  return c.Error(422, errors.New("Oh no!"))
}
GET /oops -> [422] Oh no!

在上面的示例中,应用程序中的任何错误都将返回422状态,并将相应处理。

本文章首发在 LearnKu.com 网站上。
上一篇 下一篇
superwen
讨论数量: 0
发起讨论 只看当前版本


暂无话题~