zitadel/internal/api/grpc/caos_errors.go

47 lines
1.4 KiB
Go
Raw Normal View History

2020-03-23 07:01:59 +01:00
package grpc
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
caos_errs "github.com/caos/zitadel/internal/errors"
)
func CaosToGRPCError(err error) error {
if err == nil {
return nil
}
2020-03-30 13:17:49 +02:00
code, msg, ok := Extract(err)
2020-03-23 07:01:59 +01:00
if !ok {
return status.Convert(err).Err()
}
return status.Error(code, msg)
}
2020-03-30 13:17:49 +02:00
func Extract(err error) (c codes.Code, msg string, ok bool) {
2020-03-23 07:01:59 +01:00
switch caosErr := err.(type) {
case *caos_errs.AlreadyExistsError:
return codes.AlreadyExists, caosErr.GetMessage(), true
case *caos_errs.DeadlineExceededError:
return codes.DeadlineExceeded, caosErr.GetMessage(), true
case caos_errs.InternalError:
return codes.Internal, caosErr.GetMessage(), true
case *caos_errs.InvalidArgumentError:
return codes.InvalidArgument, caosErr.GetMessage(), true
case *caos_errs.NotFoundError:
return codes.NotFound, caosErr.GetMessage(), true
case *caos_errs.PermissionDeniedError:
return codes.PermissionDenied, caosErr.GetMessage(), true
case *caos_errs.PreconditionFailedError:
return codes.FailedPrecondition, caosErr.GetMessage(), true
case *caos_errs.UnauthenticatedError:
return codes.Unauthenticated, caosErr.GetMessage(), true
case *caos_errs.UnavailableError:
return codes.Unavailable, caosErr.GetMessage(), true
case *caos_errs.UnimplementedError:
return codes.Unimplemented, caosErr.GetMessage(), true
default:
return codes.Unknown, err.Error(), false
}
}