The new Ripple frontend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

recovery.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. raven "github.com/getsentry/raven-go"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // Recovery is a better sentry logger.
  13. func Recovery(client *raven.Client, onlyCrashes bool) gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. var requestBody []byte
  16. defer func() {
  17. st := raven.NewStacktrace(0, 3, []string{"git.zxq.co/ripple"})
  18. tokenRaw, ex := c.Get("token")
  19. var token string
  20. if ex {
  21. token = tokenRaw.(string)
  22. }
  23. ravenHTTP := raven.NewHttp(c.Request)
  24. if len(requestBody) != 0 {
  25. ravenHTTP.Data = string(requestBody)
  26. }
  27. ravenUser := &raven.User{
  28. Username: token,
  29. IP: c.Request.RemoteAddr,
  30. }
  31. flags := map[string]string{
  32. "endpoint": c.Request.RequestURI,
  33. "token": token,
  34. }
  35. if rval := recover(); rval != nil {
  36. var err error
  37. switch rval := rval.(type) {
  38. case string:
  39. err = errors.New(rval)
  40. case error:
  41. err = rval
  42. default:
  43. err = fmt.Errorf("%v - %#v", rval, rval)
  44. }
  45. fmt.Println(err)
  46. client.CaptureError(
  47. err,
  48. flags,
  49. st,
  50. ravenHTTP,
  51. ravenUser,
  52. )
  53. c.AbortWithStatus(http.StatusInternalServerError)
  54. }
  55. if !onlyCrashes {
  56. for _, item := range c.Errors {
  57. var err = error(item)
  58. if item.Type == gin.ErrorTypePrivate {
  59. err = item.Err
  60. }
  61. fmt.Println(err)
  62. client.CaptureError(
  63. err,
  64. flags,
  65. ravenHTTP,
  66. ravenUser,
  67. )
  68. }
  69. }
  70. }()
  71. if c.Request.Method == "POST" && c.Request.URL.Path != "/tokens" &&
  72. c.Request.URL.Path != "/tokens/new" {
  73. var err error
  74. requestBody, err = ioutil.ReadAll(c.Request.Body)
  75. if err != nil {
  76. c.Error(err)
  77. }
  78. c.Request.Body = fakeBody{
  79. r: bytes.NewReader(requestBody),
  80. orig: c.Request.Body,
  81. }
  82. }
  83. c.Next()
  84. }
  85. }
  86. type fakeBody struct {
  87. r io.Reader
  88. orig io.ReadCloser
  89. }
  90. func (f fakeBody) Read(p []byte) (int, error) { return f.r.Read(p) }
  91. func (f fakeBody) Close() error { return f.orig.Close() }