xerr.go 3.3 KB

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