From dd64cf3ebc21ab9f8c482a97e441b4920b876df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Men=C3=A9ndez?= Date: Tue, 29 Aug 2023 16:33:32 +0200 Subject: [PATCH] new errors and API documentation (#67) #65 solved --- api/README.md | 2 ++ api/censuses.go | 4 ++-- api/errors.go | 11 +++++++++++ api/tokens.go | 8 ++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/api/README.md b/api/README.md index cd041d11..54d4c7ca 100644 --- a/api/README.md +++ b/api/README.md @@ -77,6 +77,7 @@ Triggers a new scan for the provided token, starting from the defined block. | HTTP Status | Message | Internal error | |:---:|:---|:---:| | 400 | `malformed token information` | 4000 | +| 409 | `token already created` | 4009 | | 500 | `the token cannot be created` | 5000 | | 500 | `error getting token information` | 5004 | | 500 | `error initialising web3 client` | 5018 | @@ -114,6 +115,7 @@ Returns the information about the token referenced by the provided ID. | 404 | `no token found` | 4003 | | 500 | `error getting token information` | 5004 | | 500 | `error initialising web3 client` | 5018 | +| 500 | `error getting last block number from web3 endpoint` | 5021 | | 500 | `error encoding tokens` | 5011 | **MVP Warn**: If `defaultStrategy` is `0`, no strategy (neither the dummy strategy) is associated to the given token. diff --git a/api/censuses.go b/api/censuses.go index 7a06f201..a06ec569 100644 --- a/api/censuses.go +++ b/api/censuses.go @@ -42,7 +42,7 @@ func (capi *census3API) getCensus(msg *api.APIdata, ctx *httprouter.HTTPContext) // begin a transaction for group sql queries tx, err := capi.db.BeginTx(internalCtx, nil) if err != nil { - return err + return ErrCantGetCensus } defer func() { if err := tx.Rollback(); err != nil { @@ -96,7 +96,7 @@ func (capi *census3API) createAndPublishCensus(msg *api.APIdata, ctx *httprouter // begin a transaction for group sql queries tx, err := capi.db.BeginTx(internalCtx, nil) if err != nil { - return err + return ErrCantCreateCensus } defer func() { if err := tx.Rollback(); err != nil { diff --git a/api/errors.go b/api/errors.go index 3d5a49d9..adbf0b5e 100644 --- a/api/errors.go +++ b/api/errors.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "net/http" "go.vocdoni.io/dvote/httprouter/apirest" ) @@ -52,6 +53,11 @@ var ( HTTPstatus: apirest.HTTPstatusNoContent, Err: fmt.Errorf("no strategy found"), } + ErrTokenAlreadyExists = apirest.APIerror{ + Code: 4009, + HTTPstatus: http.StatusConflict, + Err: fmt.Errorf("token already created"), + } ErrCantCreateToken = apirest.APIerror{ Code: 5000, HTTPstatus: apirest.HTTPstatusInternalErr, @@ -157,4 +163,9 @@ var ( HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("error counting census size"), } + ErrCantGetLastBlockNumber = apirest.APIerror{ + Code: 5021, + HTTPstatus: apirest.HTTPstatusInternalErr, + Err: fmt.Errorf("error getting last block number from web3 endpoint"), + } ) diff --git a/api/tokens.go b/api/tokens.go index 2fbab8d3..e44b1336 100644 --- a/api/tokens.go +++ b/api/tokens.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "math/big" + "strings" "time" "github.com/ethereum/go-ethereum/common" @@ -140,6 +141,9 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex Tag: *tag, }) if err != nil { + if strings.Contains(err.Error(), "UNIQUE constraint failed") { + return ErrTokenAlreadyExists + } log.Errorw(err, "error creating token on the database") return ErrCantCreateToken.Withf("error creating token with address %s", addr) } @@ -193,12 +197,12 @@ func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext) // get last block of the network, if something fails return progress 0 w3 := state.Web3{} if err := w3.Init(internalCtx, capi.web3, address, state.TokenType(tokenData.TypeID)); err != nil { - return ErrInitializingWeb3 + return ErrInitializingWeb3.WithErr(err) } // fetch the last block header and calculate progress lastBlockNumber, err := w3.LatestBlockNumber(internalCtx) if err != nil { - return ErrCantGetToken + return ErrCantGetLastBlockNumber } tokenProgress = uint64(float64(atBlock) / float64(lastBlockNumber) * 100) }