Skip to content

Commit

Permalink
Better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HirbodBehnam committed Dec 28, 2023
1 parent be8d850 commit b3d7745
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions payment/api/goods.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (api *API) GetGoods(c *gin.Context) {
goods, err := api.Database.GetGoods()
if err != nil {
log.WithError(err).Error("cannot get goods")
c.JSON(http.StatusInternalServerError, err.Error())
c.JSON(http.StatusInternalServerError, "cannot get goods: "+err.Error())
return
}
c.JSON(http.StatusOK, goods)
Expand Down Expand Up @@ -43,7 +43,7 @@ func (api *API) AddGood(c *gin.Context) {
c.JSON(http.StatusConflict, "good already exists")
} else {
logger.WithError(err).Error("cannot insert body in database")
c.JSON(http.StatusInternalServerError, err.Error())
c.JSON(http.StatusInternalServerError, "cannot insert body in database: "+err.Error())
}
return
}
Expand Down
6 changes: 3 additions & 3 deletions payment/api/idpay.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (api *API) CreateTransaction(c *gin.Context) {
c.JSON(http.StatusBadRequest, err.Error())
} else {
logger.WithError(err).Error("cannot query goods")
c.JSON(http.StatusInternalServerError, err.Error())
c.JSON(http.StatusInternalServerError, "cannot query goods: "+err.Error())
}
return
}
Expand All @@ -50,7 +50,7 @@ func (api *API) CreateTransaction(c *gin.Context) {
err = api.Database.InitiateTransaction(&payment)
if err != nil {
logger.WithError(err).Error("cannot put the transaction in database")
c.JSON(http.StatusInternalServerError, err.Error())
c.JSON(http.StatusInternalServerError, "cannot put the transaction in database: "+err.Error())
return
}
// Initiate the request in idpay
Expand All @@ -65,7 +65,7 @@ func (api *API) CreateTransaction(c *gin.Context) {
})
if err != nil {
logger.WithError(err).Error("cannot start idpay transaction")
c.JSON(http.StatusInternalServerError, err.Error())
c.JSON(http.StatusInternalServerError, "cannot start idpay transaction: "+err.Error())
// Mark the transaction in database as failed
api.Database.MarkAsFailed(payment.OrderID)
return
Expand Down
2 changes: 1 addition & 1 deletion payment/internal/database/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (db PaymentDatabase) GetGoodsFromName(names []string) ([]Good, error) {
// TODO: There SHOULD be a better way
result := make([]Good, len(names))
for i, name := range names {
if err := db.db.Where("name = ?", name).Find(&result[i]).Error; err != nil {
if err := db.db.Where("name = ?", name).First(&result[i]).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, GoodNotFoundError{GoodName: name}
} else {
Expand Down

0 comments on commit b3d7745

Please sign in to comment.