rbd: use internal as default error code in getGRPCError()

This commit replaces codes.Unknown with codes.Internal
as the default error code in getGRPCError().

Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
Rakshith R 2024-06-10 15:27:05 +05:30 committed by mergify[bot]
parent f8b9bc02be
commit ec8017512f
4 changed files with 11 additions and 43 deletions

View File

@ -776,14 +776,10 @@ func getGRPCError(err error) error {
} }
errorStatusMap := map[error]codes.Code{ errorStatusMap := map[error]codes.Code{
corerbd.ErrFetchingLocalState: codes.Internal, corerbd.ErrInvalidArgument: codes.InvalidArgument,
corerbd.ErrResyncImageFailed: codes.Internal, corerbd.ErrAborted: codes.Aborted,
corerbd.ErrDisableImageMirroringFailed: codes.Internal, corerbd.ErrFailedPrecondition: codes.FailedPrecondition,
corerbd.ErrFetchingMirroringInfo: codes.Internal, corerbd.ErrUnavailable: codes.Unavailable,
corerbd.ErrInvalidArgument: codes.InvalidArgument,
corerbd.ErrAborted: codes.Aborted,
corerbd.ErrFailedPrecondition: codes.FailedPrecondition,
corerbd.ErrUnavailable: codes.Unavailable,
} }
for e, code := range errorStatusMap { for e, code := range errorStatusMap {
@ -792,8 +788,8 @@ func getGRPCError(err error) error {
} }
} }
// Handle any other non nil error not listed in the map // Handle any other non nil error not listed in the map as internal error
return status.Error(codes.Unknown, err.Error()) return status.Error(codes.Internal, err.Error())
} }
// GetVolumeReplicationInfo extracts the RBD volume information from the volumeID, If the // GetVolumeReplicationInfo extracts the RBD volume information from the volumeID, If the

View File

@ -541,26 +541,6 @@ func TestGetGRPCError(t *testing.T) {
err error err error
expectedErr error expectedErr error
}{ }{
{
name: "FetchingLocalStateFailed",
err: corerbd.ErrFetchingLocalState,
expectedErr: status.Error(codes.Internal, corerbd.ErrFetchingLocalState.Error()),
},
{
name: "ResyncImageFailed",
err: corerbd.ErrResyncImageFailed,
expectedErr: status.Error(codes.Internal, corerbd.ErrResyncImageFailed.Error()),
},
{
name: "DisableImageMirroringFailed",
err: corerbd.ErrDisableImageMirroringFailed,
expectedErr: status.Error(codes.Internal, corerbd.ErrDisableImageMirroringFailed.Error()),
},
{
name: "FetchingMirroringInfoFailed",
err: corerbd.ErrFetchingMirroringInfo,
expectedErr: status.Error(codes.Internal, corerbd.ErrFetchingMirroringInfo.Error()),
},
{ {
name: "InvalidArgument", name: "InvalidArgument",
err: corerbd.ErrInvalidArgument, err: corerbd.ErrInvalidArgument,
@ -584,7 +564,7 @@ func TestGetGRPCError(t *testing.T) {
{ {
name: "InvalidError", name: "InvalidError",
err: errors.New("some error"), err: errors.New("some error"),
expectedErr: status.Error(codes.Unknown, "some error"), expectedErr: status.Error(codes.Internal, "some error"),
}, },
{ {
name: "NilError", name: "NilError",

View File

@ -55,14 +55,6 @@ var (
ErrAborted = errors.New("operation got aborted") ErrAborted = errors.New("operation got aborted")
// ErrInvalidArgument is returned when the client specified an invalid argument. // ErrInvalidArgument is returned when the client specified an invalid argument.
ErrInvalidArgument = errors.New("invalid arguments provided") ErrInvalidArgument = errors.New("invalid arguments provided")
// ErrFetchingLocalState is returned when the operation to fetch local state fails.
ErrFetchingLocalState = errors.New("failed to get local state")
// ErrDisableImageMirroringFailed is returned when the operation to disable image mirroring fails.
ErrDisableImageMirroringFailed = errors.New("failed to disable image mirroring")
// ErrFetchingMirroringInfo is returned when the operation to fetch mirroring info of image fails.
ErrFetchingMirroringInfo = errors.New("failed to get mirroring info of image")
// ErrResyncImageFailed is returned when the operation to resync the image fails.
ErrResyncImageFailed = errors.New("failed to resync image")
// ErrImageInUse is returned when the image is in use. // ErrImageInUse is returned when the image is in use.
ErrImageInUse = errors.New("image is in use") ErrImageInUse = errors.New("image is in use")
) )

View File

@ -25,7 +25,7 @@ import (
func (rv *rbdVolume) ResyncVol(localStatus librbd.SiteMirrorImageStatus) error { func (rv *rbdVolume) ResyncVol(localStatus librbd.SiteMirrorImageStatus) error {
if err := rv.resyncImage(); err != nil { if err := rv.resyncImage(); err != nil {
return fmt.Errorf("%w: failed to resync image: %w", ErrResyncImageFailed, err) return fmt.Errorf("failed to resync image: %w", err)
} }
// If we issued a resync, return a non-final error as image needs to be recreated // If we issued a resync, return a non-final error as image needs to be recreated
@ -73,7 +73,7 @@ func (rv *rbdVolume) DisableVolumeReplication(
// replication Kubernetes artifacts after failback operation. // replication Kubernetes artifacts after failback operation.
localStatus, rErr := rv.GetLocalState() localStatus, rErr := rv.GetLocalState()
if rErr != nil { if rErr != nil {
return fmt.Errorf("%w: %w", ErrFetchingLocalState, rErr) return fmt.Errorf("failed to get local state: %w", rErr)
} }
if localStatus.Up && localStatus.State == librbd.MirrorImageStatusStateReplaying { if localStatus.Up && localStatus.State == librbd.MirrorImageStatusStateReplaying {
return nil return nil
@ -84,13 +84,13 @@ func (rv *rbdVolume) DisableVolumeReplication(
} }
err := rv.DisableImageMirroring(force) err := rv.DisableImageMirroring(force)
if err != nil { if err != nil {
return fmt.Errorf("%w: %w", ErrDisableImageMirroringFailed, err) return fmt.Errorf("failed to disable image mirroring: %w", err)
} }
// the image state can be still disabling once we disable the mirroring // the image state can be still disabling once we disable the mirroring
// check the mirroring is disabled or not // check the mirroring is disabled or not
mirroringInfo, err = rv.GetImageMirroringInfo() mirroringInfo, err = rv.GetImageMirroringInfo()
if err != nil { if err != nil {
return fmt.Errorf("%w: %w", ErrFetchingMirroringInfo, err) return fmt.Errorf("failed to get mirroring info of image: %w", err)
} }
if mirroringInfo.State == librbd.MirrorImageDisabling { if mirroringInfo.State == librbd.MirrorImageDisabling {
return fmt.Errorf("%w: %q is in disabling state", ErrAborted, rv.VolID) return fmt.Errorf("%w: %q is in disabling state", ErrAborted, rv.VolID)