Skip to content

Commit

Permalink
replace error logs with error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Sep 6, 2023
1 parent 84c5788 commit fb79b3c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 102 deletions.
78 changes: 28 additions & 50 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,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 {
log.Errorw(err, "error starting database")
return ErrCantGetCensus
return ErrCantGetCensus.WithErr(err)
}
defer func() {
if err := tx.Rollback(); err != nil {
Expand All @@ -57,16 +56,14 @@ func (capi *census3API) getCensus(msg *api.APIdata, ctx *httprouter.HTTPContext)
qtx := capi.sqlc.WithTx(tx)
currentCensus, err := qtx.CensusByID(internalCtx, int64(censusID))
if err != nil {
log.Errorw(err, "error getting census from database")
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFoundCensus
return ErrNotFoundCensus.WithErr(err)
}
return ErrCantGetCensus
return ErrCantGetCensus.WithErr(err)
}
chainID, err := qtx.ChainID(internalCtx)
if err != nil {
log.Errorw(err, "error getting chainID")
return ErrCantGetCensus
return ErrCantGetCensus.WithErr(err)
}
censusSize := int32(0)
if currentCensus.Size.Valid {
Expand All @@ -87,8 +84,7 @@ func (capi *census3API) getCensus(msg *api.APIdata, ctx *httprouter.HTTPContext)
Anonymous: currentCensus.CensusType == int64(census.AnonymousCensusType),
})
if err != nil {
log.Errorw(err, "error encoding census")
return ErrEncodeCensus
return ErrEncodeCensus.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
Expand All @@ -101,7 +97,7 @@ func (capi *census3API) launchCensusCreation(msg *api.APIdata, ctx *httprouter.H
// decode request
req := &CreateCensusResquest{}
if err := json.Unmarshal(msg.Data, req); err != nil {
return ErrMalformedStrategyID
return ErrMalformedStrategyID.WithErr(err)
}
// create and publish census merkle tree in background
queueID := capi.queue.Enqueue()
Expand All @@ -123,8 +119,7 @@ func (capi *census3API) launchCensusCreation(msg *api.APIdata, ctx *httprouter.H
QueueID: queueID,
})
if err != nil {
log.Error("error marshalling census")
return ErrEncodeCensus
return ErrEncodeCensus.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
Expand All @@ -140,8 +135,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
// begin a transaction for group sql queries
tx, err := capi.db.BeginTx(bgCtx, nil)
if err != nil {
log.Errorw(err, "error starting database")
return -1, ErrCantCreateCensus.With("error starting database")
return -1, ErrCantCreateCensus.WithErr(err)
}
defer func() {
if err := tx.Rollback(); err != nil && !errors.Is(sql.ErrTxDone, err) {
Expand All @@ -153,23 +147,19 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
strategyTokens, err := qtx.TokensByStrategyID(bgCtx, int64(req.StrategyID))
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
log.Errorf("no strategy found for id %d: %s", req.StrategyID, err.Error())
return -1, ErrNoStrategyTokens.With("no strategy found")
return -1, ErrNoStrategyTokens.WithErr(err)
}
log.Errorf("error getting strategy with id %d: %s", req.StrategyID, err.Error())
return -1, ErrCantCreateCensus.With("error getting strategy")
return -1, ErrCantCreateCensus.WithErr(err)
}
if len(strategyTokens) == 0 {
log.Errorf("no tokens for strategy %d", req.StrategyID)
return -1, ErrNoStrategyTokens.With("no tokens for strategy")
return -1, ErrNoStrategyTokens.WithErr(err)
}

// get the maximun current census ID to calculate the next one, if any
// census has been created yet, continue
lastCensusID, err := qtx.LastCensusID(bgCtx)
if err != nil && !errors.Is(sql.ErrNoRows, err) {
log.Errorw(err, "error getting last census ID")
return -1, ErrCantCreateCensus.With("error getting last census ID")
return -1, ErrCantCreateCensus.WithErr(err)
}
// compute the new censusId and censusType
newCensusID := int(lastCensusID) + 1
Expand All @@ -188,8 +178,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
if errors.Is(sql.ErrNoRows, err) {
continue
}
log.Errorf("error getting token holders of %s: %v", common.BytesToAddress(token.ID), err)
return -1, ErrCantCreateCensus.With("error getting token holders")
return -1, ErrCantGetTokenHolders.WithErr(err)
}
for _, holder := range holders {
holderAddr := common.BytesToAddress(holder.ID)
Expand All @@ -208,34 +197,29 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
def := census.NewCensusDefinition(newCensusID, int(req.StrategyID), strategyHolders, req.Anonymous)
newCensus, err := capi.censusDB.CreateAndPublish(def)
if err != nil {
log.Errorf("error creating or publishing the census: %v", err)
return -1, ErrCantCreateCensus.With("error creating or publishing the census")
return -1, ErrCantCreateCensus.WithErr(err)
}
// check if the census already exists using the merkle root of the generated
// census
currentCensus, err := qtx.CensusByMerkleRoot(bgCtx, newCensus.RootHash)
if err == nil {
return int(currentCensus.ID), ErrCensusAlreadyExists
return int(currentCensus.ID), ErrCensusAlreadyExists.WithErr(err)
}
if err != nil && !errors.Is(sql.ErrNoRows, err) {
log.Errorf("error checking if the generated census already exists: %v", err)
return -1, ErrCantCreateCensus.With("error checking if the generated census already exists")
return -1, ErrCantCreateCensus.WithErr(err)
}
// save the new census in the SQL database
sqlURI := new(sql.NullString)
if err := sqlURI.Scan(newCensus.URI); err != nil {
log.Errorf("error encoding census uri: %v", err)
return -1, ErrCantCreateCensus.With("error encoding census uri")
return -1, ErrCantCreateCensus.WithErr(err)
}
sqlCensusSize := sql.NullInt32{}
if err := sqlCensusSize.Scan(int64(len(strategyHolders))); err != nil {
log.Errorf("error encoding census size: %v", err)
return -1, ErrCantCreateCensus.With("error encoding census size")
return -1, ErrCantCreateCensus.WithErr(err)
}
sqlCensusWeight := sql.NullString{}
if err := sqlCensusWeight.Scan(censusWeight.String()); err != nil {
log.Errorf("error encoding census weight: %v", err)
return -1, ErrCantCreateCensus.With("error encoding census weight")
return -1, ErrCantCreateCensus.WithErr(err)
}
_, err = qtx.CreateCensus(bgCtx, queries.CreateCensusParams{
ID: int64(newCensus.ID),
Expand All @@ -248,12 +232,10 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusResquest, qID st
QueueID: qID,
})
if err != nil {
log.Errorf("error saving the census on the database: %v", err)
return -1, ErrCantCreateCensus.With("error saving the census on the database")
return -1, ErrCantCreateCensus.WithErr(err)
}
if err := tx.Commit(); err != nil {
log.Errorf("error committing the census on the database: %v", err)
return -1, ErrCantCreateCensus.With("error committing the census on the database")
return -1, ErrCantCreateCensus.WithErr(err)
}
return newCensus.ID, nil
}
Expand Down Expand Up @@ -292,14 +274,12 @@ func (capi *census3API) enqueueCensus(msg *api.APIdata, ctx *httprouter.HTTPCont
// get the census from the database by queue_id
currentCensus, err := capi.sqlc.CensusByID(internalCtx, int64(censusID))
if err != nil {
log.Errorf("error getting census by queue id: %v", err)
return ErrCantGetCensus
return ErrCantGetCensus.WithErr(err)
}
// get current chain id
chainID, err := capi.sqlc.ChainID(internalCtx)
if err != nil {
log.Errorw(err, "error getting chainID")
return ErrCantGetCensus
return ErrCantGetCensus.WithErr(err)
}
// get values for optional parameters
censusSize := int32(0)
Expand Down Expand Up @@ -327,8 +307,7 @@ func (capi *census3API) enqueueCensus(msg *api.APIdata, ctx *httprouter.HTTPCont
// encode item response and send it
res, err := json.Marshal(queueCensus)
if err != nil {
log.Errorw(ErrEncodeQueueItem, err.Error())
return ErrEncodeQueueItem
return ErrEncodeQueueItem.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
Expand All @@ -339,17 +318,17 @@ func (capi *census3API) getStrategyCensuses(msg *api.APIdata, ctx *httprouter.HT
// get strategy ID
strategyID, err := strconv.Atoi(ctx.URLParam("strategyID"))
if err != nil {
return ErrMalformedCensusID
return ErrMalformedCensusID.WithErr(err)
}
// get censuses by this strategy ID
internalCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
rows, err := capi.sqlc.CensusByStrategyID(internalCtx, int64(strategyID))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFoundCensus
return ErrNotFoundCensus.WithErr(err)
}
return ErrCantGetCensus
return ErrCantGetCensus.WithErr(err)
}
// parse and encode response
censuses := GetCensusesResponse{Censuses: []uint64{}}
Expand All @@ -358,8 +337,7 @@ func (capi *census3API) getStrategyCensuses(msg *api.APIdata, ctx *httprouter.HT
}
res, err := json.Marshal(censuses)
if err != nil {
log.Errorw(ErrEncodeCensuses, err.Error())
return ErrEncodeCensuses
return ErrEncodeCensuses.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
31 changes: 11 additions & 20 deletions api/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
queries "github.com/vocdoni/census3/db/sqlc"
"go.vocdoni.io/dvote/httprouter"
api "go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/log"
)

func (capi *census3API) initStrategiesHandlers() error {
Expand Down Expand Up @@ -65,10 +64,9 @@ func (capi *census3API) getStrategies(msg *api.APIdata, ctx *httprouter.HTTPCont
rows, err := capi.sqlc.ListStrategies(internalCtx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNoStrategies
return ErrNoStrategies.WithErr(err)
}
log.Errorw(ErrCantGetStrategies, err.Error())
return ErrCantGetStrategies
return ErrCantGetStrategies.WithErr(err)
}
if len(rows) == 0 {
return ErrNoStrategies
Expand All @@ -80,8 +78,7 @@ func (capi *census3API) getStrategies(msg *api.APIdata, ctx *httprouter.HTTPCont
}
res, err := json.Marshal(strategies)
if err != nil {
log.Errorw(ErrEncodeStrategies, err.Error())
return ErrEncodeStrategies
return ErrEncodeStrategies.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
Expand All @@ -94,19 +91,17 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex
// get provided strategyID
strategyID, err := strconv.Atoi(ctx.URLParam("strategyID"))
if err != nil {
log.Errorw(ErrMalformedStrategyID, err.Error())
return ErrMalformedStrategyID
return ErrMalformedStrategyID.WithErr(err)
}
// get strategy from the database
internalCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
strategyData, err := capi.sqlc.StrategyByID(internalCtx, int64(strategyID))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFoundStrategy
return ErrNotFoundStrategy.WithErr(err)
}
log.Errorw(ErrCantGetStrategy, err.Error())
return ErrCantGetStrategy
return ErrCantGetStrategy.WithErr(err)
}
// parse strategy information
strategy := GetStrategyResponse{
Expand All @@ -117,8 +112,7 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex
// get information of the strategy related tokens
tokensData, err := capi.sqlc.TokensByStrategyID(internalCtx, strategyData.ID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.Errorw(ErrCantGetTokens, err.Error())
return ErrCantGetTokens
return ErrCantGetTokens.WithErr(err)
}
// parse and encode tokens information
for _, tokenData := range tokensData {
Expand All @@ -131,8 +125,7 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex
}
res, err := json.Marshal(strategy)
if err != nil {
log.Errorw(ErrEncodeStrategy, err.Error())
return ErrEncodeStrategy
return ErrEncodeStrategy.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
Expand All @@ -150,10 +143,9 @@ func (capi *census3API) getTokenStrategies(msg *api.APIdata, ctx *httprouter.HTT
rows, err := capi.sqlc.StrategiesByTokenID(internalCtx, common.HexToAddress(tokenID).Bytes())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNoStrategies
return ErrNoStrategies.WithErr(err)
}
log.Errorw(ErrCantGetStrategies, err.Error())
return ErrCantGetStrategies
return ErrCantGetStrategies.WithErr(err)
}
if len(rows) == 0 {
return ErrNoStrategies
Expand All @@ -165,8 +157,7 @@ func (capi *census3API) getTokenStrategies(msg *api.APIdata, ctx *httprouter.HTT
}
res, err := json.Marshal(strategies)
if err != nil {
log.Errorw(ErrEncodeStrategies, err.Error())
return ErrEncodeStrategies
return ErrEncodeStrategies.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
Loading

0 comments on commit fb79b3c

Please sign in to comment.