123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package xerr
- import (
- "context"
- log "kpt-grpc-demo/util/logger"
- "kpt-grpc-demo/util/sentry"
- "strings"
- "kpt-grpc-demo/util/runtimeutil"
- )
- // 自定义处理过的函数放置在这里
- // ReportError 顶层函数需要主动调用这个进行错误日志上报和报警
- func ReportError(ctx context.Context, err error, messages ...string) error {
- if err == nil {
- return nil
- }
- originErr := Cause(err)
- if originErr == nil {
- return nil
- }
- // 忽略业务自定义错误
- _, ok := originErr.(*CustomError)
- if ok {
- return nil
- }
- e := Wrap(err, messages...)
- // 上报错误到 NewRelic 并且出发报警 🚔
- // _ = nrutil.NoticeError(getOperation(1), originErr)
- // 上报错误到 Sentry 记录错误
- sentry.ReportPanic(ctx, e)
- // 打个错误堆栈日志
- log.Errorf("%+v", e)
- return e
- }
- // ReportSentry 顶层函数需要主动调用这个进行错误日志上报
- func ReportSentry(ctx context.Context, err error, messages ...string) {
- if err == nil {
- return
- }
- // 忽略业务自定义错误
- _, ok := Cause(err).(*CustomError)
- if ok {
- return
- }
- // 上报错误到 Sentry 记录错误
- e := Wrap(err, messages...)
- // sentry.ReportError(ctx, e)
- // 打个错误堆栈日志
- log.Errorf("%+v", e)
- return
- }
- func getOperation(skip int, operations ...string) string {
- if len(operations) == 0 {
- return runtimeutil.CallerFuncName(skip + 1)
- }
- return operations[0]
- }
- // WithMessage annotates err with a new message.
- // If err is nil, WithMessage returns nil.
- // 如果不传 message,默认会将调用函数写入 message
- func WithMessage(err error, messages ...string) error {
- if err == nil {
- return nil
- }
- return &withMessage{
- cause: err,
- msg: buildMessage(messages...),
- }
- }
- // LogWithWrap returns an error annotating err with a stack trace
- // at the point Wrap is called, and the supplied message.
- // If err is nil, Wrap returns nil.
- // NOTE: 这个默认打错误日志哦
- func WrapWithLog(err error, messages ...string) error {
- if err == nil {
- return nil
- }
- err = &withStack{
- &withMessage{
- cause: err,
- msg: buildMessage(messages...),
- },
- callersWithErr(err),
- }
- log.Errorf("%+v", err)
- return err
- }
- // Wrap returns an error annotating err with a stack trace
- // at the point Wrap is called, and the supplied message.
- // If err is nil, Wrap returns nil.
- func Wrap(err error, messages ...string) error {
- if err == nil {
- return nil
- }
- return &withStack{
- &withMessage{
- cause: err,
- msg: buildMessage(messages...),
- },
- callersWithErr(err),
- }
- }
- func buildMessage(messages ...string) string {
- if len(messages) == 0 {
- return runtimeutil.CallerFuncPos(2)
- }
- return strings.Join(messages, " ")
- }
- // StackWithLog annotates err with a stack trace at the point WithStack was called.
- // If err is nil, WithStack returns nil.
- // NOTE: 这个默认打错误日志哦
- func StackWithLog(err error) error {
- err = wrapStack(err, 1)
- if err != nil {
- log.Errorf("%+s", err)
- }
- return err
- }
- // WithStack annotates err with a stack trace at the point WithStack was called.
- // If err is nil, WithStack returns nil.
- // NOTE: 这个没有打日志哦
- func Stack(err error) error {
- return wrapStack(err, 1)
- }
- // ErrorEqual 判断 err 是否为 errors 某一个错误
- func ErrorEqual(err error, errors ...error) bool {
- root := Cause(err)
- if root == nil {
- return false
- }
- for _, e := range errors {
- if root.Error() == e.Error() {
- return true
- }
- }
- return false
- }
|