Skip to content

Commit

Permalink
Fix: status code on bad request (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Oct 22, 2023
1 parent e77e0a9 commit 87d0ec1
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 71 deletions.
2 changes: 1 addition & 1 deletion cmd/api/handlers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetInfo() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getAccountRequest
if err := c.BindUri(&req); err != nil {
if err := c.ShouldBindUri(&req); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, Error{Message: err.Error()})
return
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/api/handlers/bigmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetBigMap() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getBigMapRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func GetBigMapHistory() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getBigMapRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -182,11 +182,11 @@ func GetBigMapKeys() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getBigMapRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}
var pageReq bigMapSearchRequest
if err := c.BindQuery(&pageReq); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&pageReq); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -238,12 +238,12 @@ func GetBigMapByKeyHash() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getBigMapByKeyHashRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

var pageReq pageableRequest
if err := c.BindQuery(&pageReq); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&pageReq); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -291,7 +291,7 @@ func GetCurrentBigMapKeyHash() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getBigMapByKeyHashRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -345,7 +345,7 @@ func GetBigMapDiffCount() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.MustGet("context").(*config.Context)
var req getBigMapRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/handlers/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func GetContractCode() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractCodeRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

if err := c.BindQuery(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/api/handlers/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func GetContract() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

var args withStatsRequest
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindQuery(&args); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

Expand Down Expand Up @@ -85,12 +85,12 @@ func GetSameContracts() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

var page pageableRequest
if err := c.BindQuery(&page); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&page); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/api/handlers/entrypoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetEntrypoints() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

Expand Down Expand Up @@ -92,11 +92,11 @@ func GetEntrypointData() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}
var reqData getEntrypointDataRequest
if err := c.BindJSON(&reqData); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindJSON(&reqData); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -144,12 +144,12 @@ func GetEntrypointSchema() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

var esReq entrypointSchemaRequest
if err := c.BindQuery(&esReq); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&esReq); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/handlers/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func ListEvents() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getAccountRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

var page pageableRequest
if err := c.BindQuery(&page); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&page); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func ForkContract(ctxs config.Contexts) gin.HandlerFunc {
return func(c *gin.Context) {
var req forkRequest
if err := c.BindJSON(&req); handleError(c, ctxs.Any().Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindJSON(&req); handleError(c, ctxs.Any().Storage, err, http.StatusBadRequest) {
return
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/api/handlers/global_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetGlobalConstant() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getGlobalConstantRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func ListGlobalConstants() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var args globalConstantsListRequest
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -110,12 +110,12 @@ func GetContractGlobalConstants() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

var args pageableRequest
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -154,11 +154,11 @@ func GetGlobalConstantContracts() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var args globalConstantsContractsRequest
if err := c.BindUri(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/handlers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func ContractsHelpers() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getByNetwork
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

var args findContract
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetMempool() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getAccountRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func NetworkMiddleware(ctxs config.Contexts) gin.HandlerFunc {
return func(c *gin.Context) {
var req getByNetwork
if err := c.BindUri(&req); err != nil {
if err := c.ShouldBindUri(&req); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, Error{Message: err.Error()})
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func GetContractMigrations() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

Expand Down
16 changes: 8 additions & 8 deletions cmd/api/handlers/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func GetOperation() gin.HandlerFunc {
any := ctxs.Any()

var req OPGRequest
if err := c.BindUri(&req); handleError(c, any.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, any.Storage, err, http.StatusBadRequest) {
return
}

var queryReq opgRequest
if err := c.BindQuery(&queryReq); handleError(c, any.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&queryReq); handleError(c, any.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -119,7 +119,7 @@ func GetImplicitOperation() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req ImplicitOperationRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -155,7 +155,7 @@ func GetOperationErrorLocation() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getOperationByIDRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -195,7 +195,7 @@ func GetOperationDiff() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getOperationByIDRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}
operation, err := ctx.Operations.GetByID(c.Request.Context(), req.ID)
Expand Down Expand Up @@ -241,12 +241,12 @@ func GetOperationGroups() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getAccountRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

var args opgForAddressRequest
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -283,7 +283,7 @@ func GetByHashAndCounter() gin.HandlerFunc {
ctxs := c.MustGet("contexts").(config.Contexts)

var req OperationGroupContentRequest
if err := c.BindUri(&req); handleError(c, ctxs.Any().Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctxs.Any().Storage, err, http.StatusNotFound) {
return
}

Expand Down
1 change: 0 additions & 1 deletion cmd/api/handlers/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ func setParatemetersWithType(params *types.Parameters, parameter *ast.TypedAst,
op.Parameters, err = tree.ToMiguel()
if err != nil {
if !tezerrors.HasGasExhaustedError(op.Errors) {
helpers.CatchErrorSentry(err)
return err
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/api/handlers/run_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func RunOperation() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}
var reqRunOp runOperationRequest
if err := c.BindJSON(&reqRunOp); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindJSON(&reqRunOp); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -135,11 +135,11 @@ func RunCode() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getContractRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}
var reqRunCode runCodeRequest
if err := c.BindJSON(&reqRunCode); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindJSON(&reqRunCode); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/handlers/smart_rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GetSmartRollup() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var req getSmartRollupRequest
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindUri(&req); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func ListSmartRollups() gin.HandlerFunc {
ctx := c.MustGet("context").(*config.Context)

var args smartRollupListRequest
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
if err := c.ShouldBindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

Expand Down
Loading

0 comments on commit 87d0ec1

Please sign in to comment.