Skip to content

Commit

Permalink
fix: handle case vault not exist (#1265)
Browse files Browse the repository at this point in the history
  • Loading branch information
trkhoi authored Dec 4, 2023
1 parent 4e524a6 commit 38e0e0c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pkg/entities/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/defipod/mochi/pkg/consts"
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/model"
baseerrs "github.com/defipod/mochi/pkg/model/errors"
"github.com/defipod/mochi/pkg/repo/vault"
vaulttxquery "github.com/defipod/mochi/pkg/repo/vault_transaction"
"github.com/defipod/mochi/pkg/request"
Expand Down Expand Up @@ -142,8 +143,12 @@ func (e *Entity) GetVault(req request.GetVaultRequest) (*model.Vault, error) {
// query db
vault, err := e.repo.Vault.GetById(int64(idInt))
if err != nil {
e.log.Fields(logger.Fields{"query": idInt}).Errorf(err, "[entity.GetVaults] repo.Vault.GetById() failed")
return nil, err
if err == gorm.ErrRecordNotFound {
return nil, baseerrs.ErrRecordNotFound
} else {
e.log.Fields(logger.Fields{"query": idInt}).Errorf(err, "[entity.GetVaults] repo.Vault.GetById() failed")
return nil, err
}
}

if req.NoFetchAmount != "true" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/handler/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/defipod/mochi/pkg/entities"
"github.com/defipod/mochi/pkg/logger"
baseerr "github.com/defipod/mochi/pkg/model/errors"
vaulttxquery "github.com/defipod/mochi/pkg/repo/vault_transaction"
"github.com/defipod/mochi/pkg/request"
"github.com/defipod/mochi/pkg/response"
Expand Down Expand Up @@ -104,7 +105,7 @@ func (h *Handler) GetVault(c *gin.Context) {
vault, err := h.entities.GetVault(req)
if err != nil {
h.log.Fields(logger.Fields{"req": req}).Error(err, "[handler.GetVault] entity.GetVault() failed")
c.JSON(http.StatusInternalServerError, response.CreateResponse[any](nil, nil, err, nil))
c.JSON(baseerr.GetStatusCode(err), response.CreateResponse[any](nil, nil, err, nil))
return
}

Expand Down

0 comments on commit 38e0e0c

Please sign in to comment.