Skip to content

Commit

Permalink
🚀 Add the new error log
Browse files Browse the repository at this point in the history
  • Loading branch information
wjuniorbh92 committed Aug 21, 2023
1 parent 1abb3b6 commit 47dff76
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 103 deletions.
73 changes: 36 additions & 37 deletions backend/internal/controller/http/v1/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type UpdateAuthFlagsRequest struct {
func (r *assetsRoutes) createAsset(c *gin.Context) {
var request CreateAssetRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()))
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

Expand All @@ -108,19 +108,19 @@ func (r *assetsRoutes) createAsset(c *gin.Context) {

sponsor, err := r.w.Get(sponsorID)
if err != nil {
errorResponse(c, http.StatusNotFound, "sponsor wallet not found")
errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err)
return
}

res, err := r.m.SendMessage(entity.CreateKeypairChannel, entity.CreateKeypairRequest{Amount: 2})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "kms messaging problems")
errorResponse(c, http.StatusInternalServerError, "kms messaging problems", err)
return
}

kpRes, ok := res.Message.(entity.CreateKeypairResponse)
if !ok || len(kpRes.PublicKeys) != 2 {
errorResponse(c, http.StatusInternalServerError, "unexpected kms response")
errorResponse(c, http.StatusInternalServerError, "unexpected kms response", err)
return
}
issuerPk := kpRes.PublicKeys[0]
Expand Down Expand Up @@ -186,12 +186,12 @@ func (r *assetsRoutes) createAsset(c *gin.Context) {
Operations: ops,
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems")
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}
_, ok = res.Message.(entity.EnvelopeResponse)
if !ok {
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response")
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err)
return
}

Expand All @@ -218,7 +218,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) {
}
asset, err = r.as.Create(asset)
if err != nil {
errorResponse(c, http.StatusNotFound, "database problems")
errorResponse(c, http.StatusNotFound, "database problems", err)
return
}

Expand All @@ -239,7 +239,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) {
func (r *assetsRoutes) mintAsset(c *gin.Context) {
var request MintAssetRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()))
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

Expand All @@ -249,13 +249,13 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) {
}
_, err := r.w.Get(request.SponsorId)
if err != nil {
errorResponse(c, http.StatusNotFound, "sponsor wallet not found")
errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err)
return
}

asset, err := r.as.GetById(request.Id)
if err != nil {
errorResponse(c, http.StatusNotFound, "asset not found")
errorResponse(c, http.StatusNotFound, "asset not found", err)
return
}
ops := []entity.Operation{
Expand All @@ -275,15 +275,14 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) {
PublicKeys: []string{asset.Issuer.Key.PublicKey},
Operations: ops,
})

if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems")
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

_, ok := res.Message.(entity.EnvelopeResponse)
if !ok {
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response")
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err)
return
}

Expand All @@ -304,13 +303,13 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) {
func (r *assetsRoutes) burnAsset(c *gin.Context) {
var request BurnAssetRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()))
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

asset, err := r.as.GetById(request.Id)
if err != nil {
errorResponse(c, http.StatusNotFound, "asset not found")
errorResponse(c, http.StatusNotFound, "asset not found", err)
return
}

Expand All @@ -320,7 +319,7 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) {
}
_, err = r.w.Get(request.SponsorId)
if err != nil {
errorResponse(c, http.StatusNotFound, "sponsor wallet not found")
errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err)
return
}
ops := []entity.Operation{
Expand All @@ -342,13 +341,13 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) {
Operations: ops,
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems")
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

_, ok := res.Message.(entity.EnvelopeResponse)
if !ok {
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response")
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err)
return
}

Expand All @@ -371,13 +370,13 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) {
func (r *assetsRoutes) transferAsset(c *gin.Context) {
var request TransferAssetRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()))
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

sourceWallet, err := r.w.Get(request.SourceWalletID)
if err != nil {
errorResponse(c, http.StatusNotFound, "source wallet not found")
errorResponse(c, http.StatusNotFound, "source wallet not found", err)
return
}

Expand All @@ -387,13 +386,13 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
}
_, err = r.w.Get(request.SponsorId)
if err != nil {
errorResponse(c, http.StatusNotFound, "sponsor wallet not found")
errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err)
return
}

asset, err := r.as.GetById(request.AssetID)
if err != nil {
errorResponse(c, http.StatusNotFound, "asset not found")
errorResponse(c, http.StatusNotFound, "asset not found", err)
return
}
ops := []entity.Operation{
Expand All @@ -415,13 +414,13 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
Operations: ops,
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems")
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

_, ok := res.Message.(entity.EnvelopeResponse)
if !ok {
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response")
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err)
return
}

Expand All @@ -442,7 +441,7 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
var request ClawbackAssetRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()))
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

Expand All @@ -452,13 +451,13 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
}
sponsor, err := r.w.Get(request.SponsorId)
if err != nil {
errorResponse(c, http.StatusNotFound, "sponsor wallet not found")
errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err)
return
}

asset, err := r.as.Get(request.Code)
if err != nil {
errorResponse(c, http.StatusNotFound, "asset not found")
errorResponse(c, http.StatusNotFound, "asset not found", err)
return
}

Expand All @@ -482,13 +481,13 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
Operations: ops,
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems")
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

_, ok := res.Message.(entity.EnvelopeResponse)
if !ok {
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response")
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err)
return
}

Expand All @@ -509,21 +508,21 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
func (r *assetsRoutes) updateAuthFlags(c *gin.Context) {
var request UpdateAuthFlagsRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()))
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

asset, err := r.as.Get(request.Code)
if err != nil {
errorResponse(c, http.StatusNotFound, "asset not found")
errorResponse(c, http.StatusNotFound, "asset not found", err)
return
}

var trustor entity.Wallet
if request.TrustorId != 0 {
trustor, err = r.w.Get(request.TrustorId)
if err != nil {
errorResponse(c, http.StatusNotFound, "trustor wallet not found")
errorResponse(c, http.StatusNotFound, "trustor wallet not found", err)
return
}
} else if request.TrustorPK != "" {
Expand All @@ -544,7 +543,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) {

sponsor, err := r.w.Get(_sponsorId)
if err != nil {
errorResponse(c, http.StatusNotFound, "sponsor wallet not found")
errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err)
return
}

Expand All @@ -554,13 +553,13 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) {
Operations: []entity.Operation{op},
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems")
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

_, ok := res.Message.(entity.EnvelopeResponse)
if !ok {
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response")
errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err)
return
}

Expand All @@ -578,7 +577,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) {
func (r *assetsRoutes) getAllAssets(c *gin.Context) {
assets, err := r.as.GetAll()
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error getting assets")
errorResponse(c, http.StatusInternalServerError, "error getting assets", err)
return
}

Expand All @@ -597,7 +596,7 @@ func (r *assetsRoutes) getAssetById(c *gin.Context) {
assetId := c.Param("id")
assets, err := r.as.GetById(assetId)
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error getting asset")
errorResponse(c, http.StatusInternalServerError, "error getting asset", err)
return
}

Expand Down
19 changes: 10 additions & 9 deletions backend/internal/controller/http/v1/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type contractRoutes struct {
}

func newContractRoutes(handler *gin.RouterGroup, m HTTPControllerMessenger, a usecase.AuthUseCase, c usecase.ContractUseCase, v usecase.VaultUseCase,
as usecase.AssetUseCase) {
as usecase.AssetUseCase,
) {
r := &contractRoutes{m, a, c, v, as}
h := handler.Group("/contract").Use(Auth(r.a.ValidateToken()))
{
Expand Down Expand Up @@ -56,25 +57,25 @@ func (r *contractRoutes) createContract(c *gin.Context) {
var err error

if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()))
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

asset, err := r.as.GetById(request.AssetId)
if err != nil {
errorResponse(c, http.StatusNotFound, "asset not found")
errorResponse(c, http.StatusNotFound, "asset not found", err)
return
}

vaultId, err := strconv.Atoi(request.VaultId)
if err != nil {
errorResponse(c, http.StatusBadRequest, "invalid vault ID")
errorResponse(c, http.StatusBadRequest, "invalid vault ID", err)
return
}

vault, err := r.v.GetById(vaultId)
if err != nil {
errorResponse(c, http.StatusNotFound, "vault not found")
errorResponse(c, http.StatusNotFound, "vault not found", err)
return
}

Expand All @@ -91,7 +92,7 @@ func (r *contractRoutes) createContract(c *gin.Context) {

contract, err = r.c.Create(contract)
if err != nil {
errorResponse(c, http.StatusNotFound, fmt.Sprintf("error: %s", err.Error()))
errorResponse(c, http.StatusNotFound, fmt.Sprintf("error: %s", err.Error()), err)
return
}

Expand All @@ -109,7 +110,7 @@ func (r *contractRoutes) createContract(c *gin.Context) {
func (r *contractRoutes) getAllContracts(c *gin.Context) {
contract, err := r.v.GetAll()
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error getting contract")
errorResponse(c, http.StatusInternalServerError, "error getting contract", err)
return
}

Expand All @@ -129,12 +130,12 @@ func (r *contractRoutes) getContractById(c *gin.Context) {

vaultId, err := strconv.Atoi(idStr)
if err != nil {
errorResponse(c, http.StatusBadRequest, "invalid vault ID")
errorResponse(c, http.StatusBadRequest, "invalid vault ID", err)
return
}
contract, err := r.v.GetById(vaultId)
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error getting contract")
errorResponse(c, http.StatusInternalServerError, "error getting contract", err)
return
}

Expand Down
7 changes: 5 additions & 2 deletions backend/internal/controller/http/v1/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type response struct {
Error string `json:"error" example:"message"`
}

func errorResponse(c *gin.Context, code int, msg string) {
c.AbortWithStatusJSON(code, response{msg})
func errorResponse(c *gin.Context, code int, msg string, err error) {
c.AbortWithStatusJSON(code, gin.H{
"message": msg,
"error": err,
})
}
2 changes: 1 addition & 1 deletion backend/internal/controller/http/v1/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newRoleRoutes(handler *gin.RouterGroup, roleUseCase usecase.RoleUseCase, me
func (r *role) list(c *gin.Context) {
roles, err := r.roleUseCase.List()
if err != nil {
errorResponse(c, http.StatusInternalServerError, "database problems")
errorResponse(c, http.StatusInternalServerError, "database problems", err)
}
c.JSON(http.StatusOK, roles)
}
2 changes: 1 addition & 1 deletion backend/internal/controller/http/v1/role_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *rolePermissions) rolePermissions(c *gin.Context) {
token := c.GetHeader("Authorization")
rolePermissions, err := r.rolePermissionUseCase.GetRolePermissions(token)
if err != nil {
errorResponse(c, http.StatusInternalServerError, "database problems")
errorResponse(c, http.StatusInternalServerError, "database problems", err)
}
c.JSON(http.StatusOK, rolePermissions)
}
Loading

0 comments on commit 47dff76

Please sign in to comment.