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