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

loggerInterceptor.go 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package rpcserver
  13. import (
  14. "context"
  15. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/helper/xerr"
  16. "github.com/pkg/errors"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. "google.golang.org/grpc"
  19. "google.golang.org/grpc/codes"
  20. "google.golang.org/grpc/status"
  21. )
  22. // LoggerInterceptor rpc service logger interceptor
  23. func LoggerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  24. resp, err = handler(ctx, req)
  25. if err != nil {
  26. causeErr := errors.Cause(err) // err类型
  27. if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型
  28. logx.WithContext(ctx).Errorf("【RPC-SRV-ERR】 %+v", err)
  29. //转成grpc err
  30. err = status.Error(codes.Code(e.GetErrCode()), e.GetErrMsg())
  31. } else {
  32. logx.WithContext(ctx).Errorf("【RPC-SRV-ERR】 %+v", err)
  33. }
  34. }
  35. return resp, err
  36. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.