diff --git a/core/controller/cluster/controller_test.go b/core/controller/cluster/controller_test.go index 41c0efd1..4887a1c2 100644 --- a/core/controller/cluster/controller_test.go +++ b/core/controller/cluster/controller_test.go @@ -500,6 +500,7 @@ func TestAll(t *testing.T) { t.Run("Test", test) t.Run("TestV2", testV2) t.Run("TestUpgrade", testUpgrade) + t.Run("TestListClusterByNameFuzzily", testListClusterByNameFuzzily) t.Run("TestGetClusterOutPut", testGetClusterOutPut) t.Run("TestRenderOutPutObject", testRenderOutPutObject) t.Run("TestRenderOutPutObjectMissingKey", testRenderOutPutObjectMissingKey) @@ -508,7 +509,6 @@ func TestAll(t *testing.T) { t.Run("TestControllerFreeOrDeleteClusterFailed", testControllerFreeOrDeleteClusterFailed) t.Run("TestImageURL", testImageURL) t.Run("TestPinyin", testPinyin) - t.Run("TestListClusterByNameFuzzily", testListClusterByNameFuzzily) t.Run("TestListUserClustersByNameFuzzily", testListUserClustersByNameFuzzily) t.Run("TestListClusterWithExpiry", testListClusterWithExpiry) t.Run("TestGetClusterStatusV2", testGetClusterStatusV2) diff --git a/core/http/api/v2/badge/apis.go b/core/http/api/v2/badge/apis.go index b490922c..29080d3f 100644 --- a/core/http/api/v2/badge/apis.go +++ b/core/http/api/v2/badge/apis.go @@ -165,6 +165,10 @@ func (a *API) Get(c *gin.Context) { if badgeName == "" { if b, err := a.badgeCtl.GetBadge(c, uint(badgeID)); err != nil { + if _, ok := perror.Cause(err).(*herrors.HorizonErrNotFound); ok { + response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error())) + return + } response.AbortWithRPCError(c, rpcerror.InternalError.WithErrMsg(fmt.Sprintf("failed to get badge, err: %s", err.Error()))) } else { @@ -172,6 +176,10 @@ func (a *API) Get(c *gin.Context) { } } else { if b, err := a.badgeCtl.GetBadgeByName(c, resourceType, resourceID, badgeName); err != nil { + if _, ok := perror.Cause(err).(*herrors.HorizonErrNotFound); ok { + response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error())) + return + } response.AbortWithRPCError(c, rpcerror.InternalError.WithErrMsg(fmt.Sprintf("failed to get badge, err: %s", err.Error()))) } else { diff --git a/pkg/badge/dao/dao.go b/pkg/badge/dao/dao.go index 06850169..8dffe346 100644 --- a/pkg/badge/dao/dao.go +++ b/pkg/badge/dao/dao.go @@ -17,6 +17,7 @@ package dao import ( "context" + "github.com/pkg/errors" "gorm.io/gorm" herrors "github.com/horizoncd/horizon/core/errors" @@ -89,6 +90,9 @@ func (d *dao) UpdateByName(ctx context.Context, resourceType string, func (d *dao) Get(ctx context.Context, id uint) (*models.Badge, error) { var badge models.Badge if err := d.db.WithContext(ctx).First(&badge, id).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, herrors.NewErrNotFound(herrors.BadgeInDB, err.Error()) + } return nil, herrors.NewErrGetFailed(herrors.BadgeInDB, err.Error()) } return &badge, nil @@ -99,6 +103,9 @@ func (d *dao) GetByName(ctx context.Context, resourceType string, resourceID uin if err := d.db.WithContext(ctx).Where( "resource_type = ? AND resource_id = ? AND name = ?", resourceType, resourceID, name).First(&badge).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, herrors.NewErrNotFound(herrors.BadgeInDB, err.Error()) + } return nil, herrors.NewErrGetFailed(herrors.BadgeInDB, err.Error()) } return &badge, nil