xerr.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package xerr
  2. import (
  3. "context"
  4. log "kpt-event/util/logger"
  5. "kpt-event/util/sentry"
  6. "strings"
  7. "kpt-event/util/runtimeutil"
  8. )
  9. // 自定义处理过的函数放置在这里
  10. // ReportError 顶层函数需要主动调用这个进行错误日志上报和报警
  11. func ReportError(ctx context.Context, err error, messages ...string) error {
  12. if err == nil {
  13. return nil
  14. }
  15. originErr := Cause(err)
  16. if originErr == nil {
  17. return nil
  18. }
  19. // 忽略业务自定义错误
  20. _, ok := originErr.(*CustomError)
  21. if ok {
  22. return nil
  23. }
  24. e := Wrap(err, messages...)
  25. // 上报错误到 NewRelic 并且出发报警 🚔
  26. // _ = nrutil.NoticeError(getOperation(1), originErr)
  27. // 上报错误到 Sentry 记录错误
  28. sentry.ReportPanic(ctx, e)
  29. // 打个错误堆栈日志
  30. log.Errorf("%+v", e)
  31. return e
  32. }
  33. // ReportSentry 顶层函数需要主动调用这个进行错误日志上报
  34. func ReportSentry(ctx context.Context, err error, messages ...string) {
  35. if err == nil {
  36. return
  37. }
  38. // 忽略业务自定义错误
  39. _, ok := Cause(err).(*CustomError)
  40. if ok {
  41. return
  42. }
  43. // 上报错误到 Sentry 记录错误
  44. e := Wrap(err, messages...)
  45. // sentry.ReportError(ctx, e)
  46. // 打个错误堆栈日志
  47. log.Errorf("%+v", e)
  48. return
  49. }
  50. func getOperation(skip int, operations ...string) string {
  51. if len(operations) == 0 {
  52. return runtimeutil.CallerFuncName(skip + 1)
  53. }
  54. return operations[0]
  55. }
  56. // WithMessage annotates err with a new message.
  57. // If err is nil, WithMessage returns nil.
  58. // 如果不传 message,默认会将调用函数写入 message
  59. func WithMessage(err error, messages ...string) error {
  60. if err == nil {
  61. return nil
  62. }
  63. return &withMessage{
  64. cause: err,
  65. msg: buildMessage(messages...),
  66. }
  67. }
  68. // LogWithWrap returns an error annotating err with a stack trace
  69. // at the point Wrap is called, and the supplied message.
  70. // If err is nil, Wrap returns nil.
  71. // NOTE: 这个默认打错误日志哦
  72. func WrapWithLog(err error, messages ...string) error {
  73. if err == nil {
  74. return nil
  75. }
  76. err = &withStack{
  77. &withMessage{
  78. cause: err,
  79. msg: buildMessage(messages...),
  80. },
  81. callersWithErr(err),
  82. }
  83. log.Errorf("%+v", err)
  84. return err
  85. }
  86. // Wrap returns an error annotating err with a stack trace
  87. // at the point Wrap is called, and the supplied message.
  88. // If err is nil, Wrap returns nil.
  89. func Wrap(err error, messages ...string) error {
  90. if err == nil {
  91. return nil
  92. }
  93. return &withStack{
  94. &withMessage{
  95. cause: err,
  96. msg: buildMessage(messages...),
  97. },
  98. callersWithErr(err),
  99. }
  100. }
  101. func buildMessage(messages ...string) string {
  102. if len(messages) == 0 {
  103. return runtimeutil.CallerFuncPos(2)
  104. }
  105. return strings.Join(messages, " ")
  106. }
  107. // StackWithLog annotates err with a stack trace at the point WithStack was called.
  108. // If err is nil, WithStack returns nil.
  109. // NOTE: 这个默认打错误日志哦
  110. func StackWithLog(err error) error {
  111. err = wrapStack(err, 1)
  112. if err != nil {
  113. log.Errorf("%+s", err)
  114. }
  115. return err
  116. }
  117. // WithStack annotates err with a stack trace at the point WithStack was called.
  118. // If err is nil, WithStack returns nil.
  119. // NOTE: 这个没有打日志哦
  120. func Stack(err error) error {
  121. return wrapStack(err, 1)
  122. }
  123. // ErrorEqual 判断 err 是否为 errors 某一个错误
  124. func ErrorEqual(err error, errors ...error) bool {
  125. root := Cause(err)
  126. if root == nil {
  127. return false
  128. }
  129. for _, e := range errors {
  130. if root.Error() == e.Error() {
  131. return true
  132. }
  133. }
  134. return false
  135. }