Skip to content

Commit

Permalink
MG-1967 - Handle errors in Things gRPC server and client (#257)
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 authored Jan 10, 2024
1 parent 7378156 commit 2355361
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
35 changes: 33 additions & 2 deletions things/api/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package grpc

import (
"context"
"fmt"
"time"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/pkg/errors"
"github.com/go-kit/kit/endpoint"
kitgrpc "github.com/go-kit/kit/transport/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const svcName = "magistrala.AuthzService"
Expand Down Expand Up @@ -44,11 +48,11 @@ func (client grpcClient) Authorize(ctx context.Context, req *magistrala.Authoriz

res, err := client.authorize(ctx, req)
if err != nil {
return &magistrala.AuthorizeRes{}, err
return &magistrala.AuthorizeRes{}, decodeError(err)
}

ar := res.(authorizeRes)
return &magistrala.AuthorizeRes{Authorized: ar.authorized, Id: ar.id}, err
return &magistrala.AuthorizeRes{Authorized: ar.authorized, Id: ar.id}, nil
}

func decodeAuthorizeResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
Expand All @@ -69,3 +73,30 @@ func encodeAuthorizeRequest(_ context.Context, grpcReq interface{}) (interface{}
Object: req.GetObject(),
}, nil
}

func decodeError(err error) error {
if st, ok := status.FromError(err); ok {
switch st.Code() {
case codes.Unauthenticated:
return errors.Wrap(errors.ErrAuthentication, errors.New(st.Message()))
case codes.PermissionDenied:
return errors.Wrap(errors.ErrAuthorization, errors.New(st.Message()))
case codes.InvalidArgument:
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
case codes.FailedPrecondition:
return errors.Wrap(errors.ErrMalformedEntity, errors.New(st.Message()))
case codes.NotFound:
return errors.Wrap(errors.ErrNotFound, errors.New(st.Message()))
case codes.AlreadyExists:
return errors.Wrap(errors.ErrConflict, errors.New(st.Message()))
case codes.OK:
if msg := st.Message(); msg != "" {
return errors.Wrap(errors.ErrUnidentified, errors.New(msg))
}
return nil
default:
return errors.Wrap(fmt.Errorf("unexpected gRPC status: %s (status code:%v)", st.Code().String(), st.Code()), errors.New(st.Message()))
}
}
return err
}
4 changes: 3 additions & 1 deletion things/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/things"
kitgrpc "github.com/go-kit/kit/transport/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -69,7 +70,8 @@ func encodeError(err error) error {
err == apiutil.ErrMissingEmail,
err == apiutil.ErrBearerToken:
return status.Error(codes.Unauthenticated, err.Error())
case errors.Contains(err, errors.ErrAuthorization):
case errors.Contains(err, errors.ErrAuthorization),
errors.Contains(err, svcerr.ErrAuthorization):
return status.Error(codes.PermissionDenied, err.Error())
default:
return status.Error(codes.Internal, err.Error())
Expand Down
8 changes: 4 additions & 4 deletions things/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func (svc service) Authorize(ctx context.Context, req *magistrala.AuthorizeReq)
}
resp, err := svc.auth.Authorize(ctx, r)
if err != nil {
return "", errors.Wrap(errors.ErrAuthorization, err)
return "", errors.Wrap(svcerr.ErrAuthorization, err)
}
if !resp.GetAuthorized() {
return "", errors.ErrAuthorization
return "", svcerr.ErrAuthorization
}

return thingID, nil
Expand Down Expand Up @@ -578,10 +578,10 @@ func (svc service) Identify(ctx context.Context, key string) (string, error) {

client, err := svc.clients.RetrieveBySecret(ctx, key)
if err != nil {
return "", errors.Wrap(repoerr.ErrNotFound, err)
return "", errors.Wrap(svcerr.ErrAuthorization, err)
}
if err := svc.clientCache.Save(ctx, key, client.ID); err != nil {
return "", errors.Wrap(repoerr.ErrUpdateEntity, err)
return "", errors.Wrap(svcerr.ErrAuthorization, err)
}

return client.ID, nil
Expand Down

0 comments on commit 2355361

Please sign in to comment.