refactor error handling in dent

This commit is contained in:
yaoandrew 2018-12-19 01:04:20 -08:00
parent 08ac7f18a5
commit 05351a8ef4

View File

@ -19,6 +19,7 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"log"
)
type DentRepository struct {
@ -26,38 +27,47 @@ type DentRepository struct {
}
func (repository DentRepository) Create(headerID int64, models []interface{}) error {
tx, err := repository.db.Begin()
if err != nil {
return err
tx, dBaseErr := repository.db.Begin()
if dBaseErr != nil {
return dBaseErr
}
tic, err := shared.GetTicInTx(headerID, tx)
if err != nil {
return err
tic, gettTicErr := shared.GetTicInTx(headerID, tx)
if gettTicErr != nil {
return gettTicErr
}
for _, model := range models {
dent, ok := model.(DentModel)
if !ok {
tx.Rollback()
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return fmt.Errorf("model of type %T, not %T", model, DentModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.dent (header_id, bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
headerID, dent.BidId, dent.Lot, dent.Bid, dent.Guy, tic, dent.LogIndex, dent.TransactionIndex, dent.Raw,
)
if err != nil {
tx.Rollback()
return err
if execErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return execErr
}
}
err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DentChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DentChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()
}