recover

Recover中间件,用于捕获各种panic异常,避免程序异常退出,但建议自定义recover中间件,在获取到此类异常时,发送告警后做graceful restart。

Example

  1. package main
  2. import (
  3. "errors"
  4. "github.com/vicanso/elton"
  5. "github.com/vicanso/elton/middleware"
  6. )
  7. func main() {
  8. e := elton.New()
  9. e.Use(middleware.NewRecover())
  10. e.GET("/", func(c *elton.Context) (err error) {
  11. panic(errors.New("abcd"))
  12. })
  13. err := e.ListenAndServe(":3000")
  14. if err != nil {
  15. panic(err)
  16. }
  17. }