Skip to content

Commit

Permalink
ocm: simplified error handling following cs3org/OCM-API#90 and cs3org…
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Aug 15, 2024
1 parent 90e93e8 commit e0a413c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
12 changes: 4 additions & 8 deletions internal/grpc/services/ocminvitemanager/ocminvitemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ func (s *service) ForwardInvite(ctx context.Context, req *invitepb.ForwardInvite
switch {
case errors.Is(err, ocmd.ErrTokenInvalid):
return &invitepb.ForwardInviteResponse{
Status: status.NewInvalid(ctx, "token not valid"),
}, nil
case errors.Is(err, ocmd.ErrTokenNotFound):
return &invitepb.ForwardInviteResponse{
Status: status.NewNotFound(ctx, "token not found"),
Status: status.NewInvalid(ctx, "token not valid or not found"),
}, nil
case errors.Is(err, ocmd.ErrUserAlreadyAccepted):
return &invitepb.ForwardInviteResponse{
Expand Down Expand Up @@ -238,9 +234,9 @@ func getOCMEndpoint(originProvider *ocmprovider.ProviderInfo) (string, error) {
func (s *service) AcceptInvite(ctx context.Context, req *invitepb.AcceptInviteRequest) (*invitepb.AcceptInviteResponse, error) {
token, err := s.repo.GetToken(ctx, req.InviteToken.Token)
if err != nil {
if errors.Is(err, invite.ErrTokenNotFound) {
if errors.Is(err, invite.ErrTokenInvalid) {
return &invitepb.AcceptInviteResponse{
Status: status.NewNotFound(ctx, "token not found"),
Status: status.NewInvalid(ctx, "token invalid or not found"),
}, nil
}
return &invitepb.AcceptInviteResponse{
Expand All @@ -250,7 +246,7 @@ func (s *service) AcceptInvite(ctx context.Context, req *invitepb.AcceptInviteRe

if !isTokenValid(token) {
return &invitepb.AcceptInviteResponse{
Status: status.NewInvalid(ctx, "token is not valid"),
Status: status.NewInvalid(ctx, "token invalid or not found"),
}, nil
}

Expand Down
9 changes: 3 additions & 6 deletions internal/http/services/experimental/sciencemesh/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,14 @@ func (h *tokenHandler) AcceptInvite(w http.ResponseWriter, r *http.Request) {
}
if forwardInviteResponse.Status.Code != rpc.Code_CODE_OK {
switch forwardInviteResponse.Status.Code {
case rpc.Code_CODE_NOT_FOUND:
reqres.WriteError(w, r, reqres.APIErrorNotFound, "token not found", nil)
return
case rpc.Code_CODE_INVALID_ARGUMENT:
reqres.WriteError(w, r, reqres.APIErrorInvalidParameter, "token has expired", nil)
reqres.WriteError(w, r, reqres.APIErrorInvalidParameter, "invalid or non existing token", nil)
return
case rpc.Code_CODE_ALREADY_EXISTS:
reqres.WriteError(w, r, reqres.APIErrorAlreadyExist, "user already known", nil)
reqres.WriteError(w, r, reqres.APIErrorAlreadyExist, "invitation already accepted", nil)
return
case rpc.Code_CODE_PERMISSION_DENIED:
reqres.WriteError(w, r, reqres.APIErrorUnauthenticated, "remove service not trusted", nil)
reqres.WriteError(w, r, reqres.APIErrorUnauthenticated, "remote service not trusted", nil)
return
default:
reqres.WriteError(w, r, reqres.APIErrorServerError, "unexpected error: "+forwardInviteResponse.Status.Message, errors.New(forwardInviteResponse.Status.Message))
Expand Down
14 changes: 4 additions & 10 deletions internal/http/services/opencloudmesh/ocmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,16 @@ import (
)

// ErrTokenInvalid is the error returned by the invite-accepted
// endpoint when the token is not valid.
var ErrTokenInvalid = errors.New("the invitation token is invalid")
// endpoint when the token is not valid or not existing.
var ErrTokenInvalid = errors.New("the invitation token is invalid or not found")

// ErrServiceNotTrusted is the error returned by the invite-accepted
// endpoint when the service is not trusted to accept invitations.
var ErrServiceNotTrusted = errors.New("service is not trusted to accept invitations")

// ErrUserAlreadyAccepted is the error returned by the invite-accepted
// endpoint when a user is already know by the remote cloud.
var ErrUserAlreadyAccepted = errors.New("user already accepted an invitation token")

// ErrTokenNotFound is the error returned by the invite-accepted
// endpoint when the request is done using a not existing token.
var ErrTokenNotFound = errors.New("token not found")
// endpoint when a token was already used by a user in the remote cloud.
var ErrUserAlreadyAccepted = errors.New("invitation already accepted")

// ErrInvalidParameters is the error returned by the shares endpoint
// when the request does not contain required properties.
Expand Down Expand Up @@ -250,8 +246,6 @@ func (c *OCMClient) parseInviteAcceptedResponse(r *http.Response) (*User, error)
return &u, nil
case http.StatusBadRequest:
return nil, ErrTokenInvalid
case http.StatusNotFound:
return nil, ErrTokenNotFound
case http.StatusConflict:
return nil, ErrUserAlreadyAccepted
case http.StatusForbidden:
Expand Down

0 comments on commit e0a413c

Please sign in to comment.