Skip to content

Commit

Permalink
grpc: add error handling wrappers to client/server
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Apr 22, 2020
1 parent 02fff48 commit 3f77f04
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 8 deletions.
71 changes: 71 additions & 0 deletions api/services/control/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package moby_buildkit_v1

import (
context "context"

"github.com/moby/buildkit/solver/errdefs"
grpc "google.golang.org/grpc"
)

func WrapServerErrors(srv ControlServer) ControlServer {
return &wrappedServer{srv}
}

type wrappedServer struct {
srv ControlServer
}

func (s *wrappedServer) DiskUsage(ctx context.Context, req *DiskUsageRequest) (*DiskUsageResponse, error) {
r, err := s.srv.DiskUsage(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (s *wrappedServer) Prune(req *PruneRequest, ps Control_PruneServer) error {
return errdefs.ToGRPC(s.srv.Prune(req, ps))
}
func (s *wrappedServer) Solve(ctx context.Context, req *SolveRequest) (*SolveResponse, error) {
r, err := s.srv.Solve(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (s *wrappedServer) Status(req *StatusRequest, ss Control_StatusServer) error {
return errdefs.ToGRPC(s.srv.Status(req, ss))
}
func (s *wrappedServer) Session(req Control_SessionServer) error {
return errdefs.ToGRPC(s.srv.Session(req))
}
func (s *wrappedServer) ListWorkers(ctx context.Context, req *ListWorkersRequest) (*ListWorkersResponse, error) {
r, err := s.srv.ListWorkers(ctx, req)
return r, errdefs.ToGRPC(err)
}

func WrapClientErrors(c ControlClient) ControlClient {
return &wrappedClient{c}
}

type wrappedClient struct {
c ControlClient
}

func (c *wrappedClient) DiskUsage(ctx context.Context, in *DiskUsageRequest, opts ...grpc.CallOption) (*DiskUsageResponse, error) {
r, err := c.c.DiskUsage(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Prune(ctx context.Context, in *PruneRequest, opts ...grpc.CallOption) (Control_PruneClient, error) {
r, err := c.c.Prune(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Solve(ctx context.Context, in *SolveRequest, opts ...grpc.CallOption) (*SolveResponse, error) {
r, err := c.c.Solve(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Status(ctx context.Context, in *StatusRequest, opts ...grpc.CallOption) (Control_StatusClient, error) {
r, err := c.c.Status(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Session(ctx context.Context, opts ...grpc.CallOption) (Control_SessionClient, error) {
r, err := c.c.Session(ctx, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) ListWorkers(ctx context.Context, in *ListWorkersRequest, opts ...grpc.CallOption) (*ListWorkersResponse, error) {
r, err := c.c.ListWorkers(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
2 changes: 1 addition & 1 deletion client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *Client) Build(ctx context.Context, opt SolveOpt, product string, buildF

func (c *Client) gatewayClientForBuild(buildid string) gatewayapi.LLBBridgeClient {
g := gatewayapi.NewLLBBridgeClient(c.conn)
return &gatewayClientForBuild{g, buildid}
return gatewayapi.WrapClientErrors(&gatewayClientForBuild{g, buildid})
}

type gatewayClientForBuild struct {
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
}

func (c *Client) controlClient() controlapi.ControlClient {
return controlapi.NewControlClient(c.conn)
return controlapi.WrapClientErrors(controlapi.NewControlClient(c.conn))
}

func (c *Client) Dialer() session.Dialer {
Expand Down
2 changes: 1 addition & 1 deletion control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewController(opt Opt) (*Controller, error) {
}

func (c *Controller) Register(server *grpc.Server) error {
controlapi.RegisterControlServer(server, c)
controlapi.RegisterControlServer(server, controlapi.WrapServerErrors(c))
c.gatewayForwarder.Register(server)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion control/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewGatewayForwarder() *GatewayForwarder {
}

func (gwf *GatewayForwarder) Register(server *grpc.Server) {
gwapi.RegisterLLBBridgeServer(server, gwf)
gwapi.RegisterLLBBridgeServer(server, gwapi.WrapServerErrors(gwf))
}

func (gwf *GatewayForwarder) RegisterBuild(ctx context.Context, id string, bridge gateway.LLBBridgeForwarder) error {
Expand Down
2 changes: 1 addition & 1 deletion frontend/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func newLLBBridgeForwarder(ctx context.Context, llbBridge frontend.FrontendLLBBr
lbf := NewBridgeForwarder(ctx, llbBridge, workers, inputs)
server := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(server, health.NewServer())
pb.RegisterLLBBridgeServer(server, lbf)
pb.RegisterLLBBridgeServer(server, pb.WrapServerErrors(lbf))

go func() {
serve(ctx, server, lbf.conn)
Expand Down
2 changes: 1 addition & 1 deletion frontend/gateway/grpcclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func current() (GrpcClient, error) {
return nil, err
}

return New(ctx, opts(), sessionID(), product(), pb.NewLLBBridgeClient(conn), workers())
return New(ctx, opts(), sessionID(), product(), pb.WrapClientErrors(pb.NewLLBBridgeClient(conn)), workers())
}

func convertRef(ref client.Reference) (*pb.Ref, error) {
Expand Down
90 changes: 90 additions & 0 deletions frontend/gateway/pb/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package moby_buildkit_v1_frontend

import (
context "context"

"github.com/moby/buildkit/solver/errdefs"
grpc "google.golang.org/grpc"
)

func WrapServerErrors(srv LLBBridgeServer) LLBBridgeServer {
return &wrappedServer{srv}
}

type wrappedServer struct {
srv LLBBridgeServer
}

func (w *wrappedServer) ResolveImageConfig(ctx context.Context, req *ResolveImageConfigRequest) (*ResolveImageConfigResponse, error) {
r, err := w.srv.ResolveImageConfig(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (w *wrappedServer) Solve(ctx context.Context, req *SolveRequest) (*SolveResponse, error) {
r, err := w.srv.Solve(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (w *wrappedServer) ReadFile(ctx context.Context, req *ReadFileRequest) (*ReadFileResponse, error) {
r, err := w.srv.ReadFile(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (w *wrappedServer) ReadDir(ctx context.Context, req *ReadDirRequest) (*ReadDirResponse, error) {
r, err := w.srv.ReadDir(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (w *wrappedServer) StatFile(ctx context.Context, req *StatFileRequest) (*StatFileResponse, error) {
r, err := w.srv.StatFile(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (w *wrappedServer) Ping(ctx context.Context, req *PingRequest) (*PongResponse, error) {
r, err := w.srv.Ping(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (w *wrappedServer) Return(ctx context.Context, req *ReturnRequest) (*ReturnResponse, error) {
r, err := w.srv.Return(ctx, req)
return r, errdefs.ToGRPC(err)
}
func (w *wrappedServer) Inputs(ctx context.Context, req *InputsRequest) (*InputsResponse, error) {
r, err := w.srv.Inputs(ctx, req)
return r, errdefs.ToGRPC(err)
}

func WrapClientErrors(c LLBBridgeClient) LLBBridgeClient {
return &wrappedClient{c}
}

type wrappedClient struct {
c LLBBridgeClient
}

func (c *wrappedClient) ResolveImageConfig(ctx context.Context, in *ResolveImageConfigRequest, opts ...grpc.CallOption) (*ResolveImageConfigResponse, error) {
r, err := c.c.ResolveImageConfig(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Solve(ctx context.Context, in *SolveRequest, opts ...grpc.CallOption) (*SolveResponse, error) {
r, err := c.c.Solve(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) ReadFile(ctx context.Context, in *ReadFileRequest, opts ...grpc.CallOption) (*ReadFileResponse, error) {
r, err := c.c.ReadFile(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) ReadDir(ctx context.Context, in *ReadDirRequest, opts ...grpc.CallOption) (*ReadDirResponse, error) {
r, err := c.c.ReadDir(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) StatFile(ctx context.Context, in *StatFileRequest, opts ...grpc.CallOption) (*StatFileResponse, error) {
r, err := c.c.StatFile(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PongResponse, error) {
r, err := c.c.Ping(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Return(ctx context.Context, in *ReturnRequest, opts ...grpc.CallOption) (*ReturnResponse, error) {
r, err := c.c.Return(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
func (c *wrappedClient) Inputs(ctx context.Context, in *InputsRequest, opts ...grpc.CallOption) (*InputsResponse, error) {
r, err := c.c.Inputs(ctx, in, opts...)
return r, errdefs.FromGRPC(err)
}
3 changes: 1 addition & 2 deletions solver/errdefs/errdefs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f77f04

Please sign in to comment.