Skip to content

Commit

Permalink
fix: Script controller conflict on reference & storage conflict error…
Browse files Browse the repository at this point in the history
… code 409 instead of 503 (#360)

* fix: script controller conflict on reference

* fix: storage conflict error code 409 instead of 503

* fix: update swagger
  • Loading branch information
Antoine Gelloz authored Nov 9, 2022
1 parent 8b23500 commit 6b265ba
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pkg/api/controllers/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const (

func coreErrorToErrorCode(err error) (int, string) {
switch {
case ledger.IsConflictError(err):
case ledger.IsConflictError(err) ||
storage.IsErrorCode(err, storage.ConstraintFailed):
return http.StatusConflict, ErrConflict
case ledger.IsInsufficientFundError(err):
return http.StatusBadRequest, ErrInsufficientFund
Expand Down
18 changes: 12 additions & 6 deletions pkg/api/controllers/script_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func (ctl *ScriptController) PostScript(c *gin.Context) {

var script core.Script
if err := c.ShouldBindJSON(&script); err != nil {
panic(err)
ResponseError(c, ledger.NewValidationError(
"invalid payload"))
return
}

value, ok := c.GetQuery("preview")
Expand All @@ -61,13 +63,17 @@ func (ctl *ScriptController) PostScript(c *gin.Context) {
code = ErrInternal
message string
)
scriptError, ok := err.(*ledger.ScriptError)
if ok {
code = scriptError.Code
message = scriptError.Message
} else {
switch e := err.(type) {
case *ledger.ScriptError:
code = e.Code
message = e.Message
case *ledger.ConflictError:
code = ErrConflict
message = e.Error()
default:
sharedlogging.GetLogger(c.Request.Context()).Errorf("internal errors executing script: %s", err)
}

res.ErrorResponse = sharedapi.ErrorResponse{
ErrorCode: code,
ErrorMessage: message,
Expand Down
42 changes: 42 additions & 0 deletions pkg/api/controllers/script_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,45 @@ func TestPostScriptWithReference(t *testing.T) {
})
}))
}

func TestPostScriptConflict(t *testing.T) {
script := `
send [COIN 100] (
source = @world
destination = @centralbank
)`

internal.RunTest(t, fx.Invoke(func(lc fx.Lifecycle, api *api.API, driver storage.Driver) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
t.Run("first should succeed", func(t *testing.T) {
rsp := internal.PostScript(t, api, core.Script{
Plain: script,
Reference: "1234",
}, url.Values{})

assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
res := controllers.ScriptResponse{}
internal.Decode(t, rsp.Body, &res)
assert.Equal(t, "", res.ErrorCode)
assert.Equal(t, "", res.ErrorMessage)
assert.NotNil(t, res.Transaction)
})

t.Run("second should fail", func(t *testing.T) {
rsp := internal.PostScript(t, api, core.Script{
Plain: script,
Reference: "1234",
}, url.Values{})

assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
res := controllers.ScriptResponse{}
internal.Decode(t, rsp.Body, &res)
assert.Equal(t, controllers.ErrConflict, res.ErrorCode)
})

return nil
},
})
}))
}
73 changes: 72 additions & 1 deletion pkg/api/controllers/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ paths:
error_message:
type: string
example: "invalid account address format"
"409":
description: Conflict
content:
application/json:
schema:
type: object
required:
- error_code
properties:
error_code:
type: string
example: "CONFLICT"
error_message:
type: string

/{ledger}/mapping:
get:
Expand Down Expand Up @@ -354,7 +368,36 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ScriptResult'

"400":
description: Bad Request
content:
application/json:
schema:
type: object
required:
- error_code
properties:
error_code:
type: string
example: "VALIDATION"
error_message:
type: string
example: "invalid payload"
"409":
description: Conflict
content:
application/json:
schema:
type: object
required:
- error_code
properties:
error_code:
type: string
example: "CONFLICT"
error_message:
type: string

/{ledger}/stats:
get:
tags:
Expand Down Expand Up @@ -749,6 +792,20 @@ paths:
error_message:
type: string
example: "transaction not found"
"409":
description: Conflict
content:
application/json:
schema:
type: object
required:
- error_code
properties:
error_code:
type: string
example: "CONFLICT"
error_message:
type: string

/{ledger}/transactions/{txid}/revert:
post:
Expand Down Expand Up @@ -808,6 +865,20 @@ paths:
error_message:
type: string
example: "transaction not found"
"409":
description: Conflict
content:
application/json:
schema:
type: object
required:
- error_code
properties:
error_code:
type: string
example: "CONFLICT"
error_message:
type: string

/{ledger}/transactions/batch:
post:
Expand Down

0 comments on commit 6b265ba

Please sign in to comment.