Skip to content

Commit

Permalink
Merge pull request #214 from kilosonc/feat/badge
Browse files Browse the repository at this point in the history
fix: response 500 for no badge found
  • Loading branch information
kilosonc authored Jan 29, 2024
2 parents e417dbd + 456bdae commit d80d8a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/controller/cluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions core/http/api/v2/badge/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,21 @@ 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 {
response.SuccessWithData(c, b)
}
} 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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/badge/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dao
import (
"context"

"github.com/pkg/errors"
"gorm.io/gorm"

herrors "github.com/horizoncd/horizon/core/errors"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d80d8a7

Please sign in to comment.