zitadel/internal/api/grpc/caos_errors.go
Livio Amstutz 3549a8b64e
feat: port reduction (#323)
* move mgmt pkg

* begin package restructure

* rename auth package to authz

* begin start api

* move auth

* move admin

* fix merge

* configs and interceptors

* interceptor

* revert generate-grpc.sh

* some cleanups

* console

* move console

* fix tests and merging

* js linting

* merge

* merging and configs

* change k8s base to current ports

* fixes

* cleanup

* regenerate proto

* remove unnecessary whitespace

* missing param

* go mod tidy

* fix merging

* move login pkg

* cleanup

* move api pkgs again

* fix pkg naming

* fix generate-static.sh for login

* update workflow

* fixes

* logging

* remove duplicate

* comment for optional gateway interfaces

* regenerate protos

* fix proto imports for grpc web

* protos

* grpc web generate

* grpc web generate

* fix changes

* add translation interceptor

* fix merging

* regenerate mgmt proto
2020-07-08 13:56:37 +02:00

64 lines
2.0 KiB
Go

package grpc
import (
"context"
"github.com/caos/logging"
"github.com/caos/zitadel/pkg/grpc/message"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/i18n"
)
func CaosToGRPCError(ctx context.Context, err error, translator *i18n.Translator) error {
if err == nil {
return nil
}
code, key, id, ok := ExtractCaosError(err)
if !ok {
return status.Convert(err).Err()
}
msg := key
if translator != nil {
msg = translator.LocalizeFromCtx(ctx, key, nil)
}
s, err := status.New(code, key).WithDetails(&message.ErrorDetail{Id: id, Message: msg})
if err != nil {
logging.Log("GRPC-gIeRw").WithError(err).Debug("unable to add detail")
return status.New(code, key).Err()
}
return s.Err()
}
func ExtractCaosError(err error) (c codes.Code, msg, id string, ok bool) {
switch caosErr := err.(type) {
case *caos_errs.AlreadyExistsError:
return codes.AlreadyExists, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.DeadlineExceededError:
return codes.DeadlineExceeded, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.InternalError:
return codes.Internal, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.InvalidArgumentError:
return codes.InvalidArgument, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.NotFoundError:
return codes.NotFound, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.PermissionDeniedError:
return codes.PermissionDenied, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.PreconditionFailedError:
return codes.FailedPrecondition, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.UnauthenticatedError:
return codes.Unauthenticated, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.UnavailableError:
return codes.Unavailable, caosErr.GetMessage(), caosErr.GetID(), true
case *caos_errs.UnimplementedError:
return codes.Unimplemented, caosErr.GetMessage(), caosErr.GetID(), true
default:
return codes.Unknown, err.Error(), "", false
}
}