Skip to content

Commit

Permalink
new errors and API documentation (#67)
Browse files Browse the repository at this point in the history
#65 solved
  • Loading branch information
lucasmenendez authored Aug 29, 2023
1 parent 7e905b0 commit dd64cf3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"net/http"

"go.vocdoni.io/dvote/httprouter/apirest"
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
}
)
8 changes: 6 additions & 2 deletions api/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"math/big"
"strings"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit dd64cf3

Please sign in to comment.