Skip to content

Commit

Permalink
Merge pull request #506 from abyssparanoia/feature/add-recovery-inter…
Browse files Browse the repository at this point in the history
…cepot

feat: add interceptor
  • Loading branch information
abyssparanoia committed Oct 6, 2022
2 parents 227313e + 66d75d5 commit 4865f42
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/infrastructure/grpc/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package grpc

import (
"context"
"fmt"
debug_util "runtime/debug"
"time"

"github.com/abyssparanoia/rapid-go/internal/infrastructure/dependency"
Expand All @@ -16,13 +18,22 @@ import (
public_apiv1 "github.com/abyssparanoia/rapid-go/internal/infrastructure/grpc/pb/rapid/public_api/v1"
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpcrecovry "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)

func recoverFuncFactory(l *zap.Logger) func(p interface{}) error {
return func(p interface{}) error {
l.Error(fmt.Sprintf("p: %+v\n : %s", p, debug_util.Stack()))
return status.Errorf(codes.Internal, "Unexpected error")
}
}

func NewServer(ctx context.Context,
e *environment.Environment,
logger *zap.Logger,
Expand Down Expand Up @@ -51,7 +62,9 @@ func NewServer(ctx context.Context,
grpc.UnaryInterceptor(
grpcmiddleware.ChainUnaryServer(
requestLogInterceptor.UnaryServerInterceptor(),
grpcrecovry.UnaryServerInterceptor(),
grpc_recovery.UnaryServerInterceptor(
grpc_recovery.WithRecoveryHandler(recoverFuncFactory(logger)),
),
grpc_auth.UnaryServerInterceptor(authFunc.Authenticate),
),
),
Expand Down

0 comments on commit 4865f42

Please sign in to comment.