2020-08-28 07:44:43 +00:00
|
|
|
package errors
|
2020-03-23 06:01:59 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-22 11:51:44 +00:00
|
|
|
"context"
|
2020-11-20 06:57:39 +00:00
|
|
|
|
2020-07-08 07:48:11 +00:00
|
|
|
"github.com/caos/logging"
|
2020-07-22 09:29:43 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/pkg/grpc/message"
|
2020-03-23 06:01:59 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2020-08-28 07:44:43 +00:00
|
|
|
func CaosToGRPCError(ctx context.Context, err error) error {
|
2020-03-23 06:01:59 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-08 07:48:11 +00:00
|
|
|
code, key, id, ok := ExtractCaosError(err)
|
2020-03-23 06:01:59 +00:00
|
|
|
if !ok {
|
|
|
|
return status.Convert(err).Err()
|
|
|
|
}
|
2020-07-08 07:48:11 +00:00
|
|
|
msg := key
|
2020-08-28 07:44:43 +00:00
|
|
|
msg += " (" + id + ")"
|
|
|
|
|
2020-07-22 09:29:43 +00:00
|
|
|
s, err := status.New(code, msg).WithDetails(&message.ErrorDetail{Id: id, Message: key})
|
2020-07-08 07:48:11 +00:00
|
|
|
if err != nil {
|
|
|
|
logging.Log("GRPC-gIeRw").WithError(err).Debug("unable to add detail")
|
2020-07-22 09:29:43 +00:00
|
|
|
return status.New(code, msg).Err()
|
2020-06-22 11:51:44 +00:00
|
|
|
}
|
2020-07-08 07:48:11 +00:00
|
|
|
|
|
|
|
return s.Err()
|
2020-03-23 06:01:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 07:48:11 +00:00
|
|
|
func ExtractCaosError(err error) (c codes.Code, msg, id string, ok bool) {
|
2020-10-21 08:18:34 +00:00
|
|
|
if err == nil {
|
|
|
|
return codes.OK, "", "", false
|
|
|
|
}
|
2020-03-23 06:01:59 +00:00
|
|
|
switch caosErr := err.(type) {
|
|
|
|
case *caos_errs.AlreadyExistsError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.AlreadyExists, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.DeadlineExceededError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.DeadlineExceeded, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-07-08 11:56:37 +00:00
|
|
|
case *caos_errs.InternalError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.Internal, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.InvalidArgumentError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.InvalidArgument, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.NotFoundError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.NotFound, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.PermissionDeniedError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.PermissionDenied, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.PreconditionFailedError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.FailedPrecondition, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.UnauthenticatedError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.Unauthenticated, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.UnavailableError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.Unavailable, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
case *caos_errs.UnimplementedError:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.Unimplemented, caosErr.GetMessage(), caosErr.GetID(), true
|
2020-03-23 06:01:59 +00:00
|
|
|
default:
|
2020-06-25 06:12:29 +00:00
|
|
|
return codes.Unknown, err.Error(), "", false
|
2020-03-23 06:01:59 +00:00
|
|
|
}
|
|
|
|
}
|