Skip to content

Commit

Permalink
fix: response 500 for no badge found
Browse files Browse the repository at this point in the history
Signed-off-by: Kiloson <[email protected]>
  • Loading branch information
kilosonc committed Jan 25, 2024
1 parent 868754f commit d9431fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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 d9431fc

Please sign in to comment.