-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
37 lines (30 loc) · 874 Bytes
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package grpc
import (
"github.com/alexfalkowski/go-service/transport/grpc"
v1 "github.com/alexfalkowski/konfig/api/konfig/v1"
"github.com/alexfalkowski/konfig/server/service"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Register server.
func Register(gs *grpc.Server, server v1.ServiceServer) {
v1.RegisterServiceServer(gs.Server(), server)
}
// NewServer for gRPC.
func NewServer(service *service.Service) v1.ServiceServer {
return &Server{service: service}
}
// Server for gRPC.
type Server struct {
v1.UnimplementedServiceServer
service *service.Service
}
func (s *Server) error(err error) error {
if service.IsInvalidArgument(err) {
return status.Error(codes.InvalidArgument, err.Error())
}
if service.IsNotFound(err) {
return status.Error(codes.NotFound, err.Error())
}
return status.Error(codes.Internal, err.Error())
}