refactor error handling in bite transformer

This commit is contained in:
yaoandrew 2018-12-18 23:50:50 -08:00
parent ebca338b1e
commit 78b302eab6

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 BiteRepository struct {
@ -30,32 +31,41 @@ func (repository *BiteRepository) SetDB(db *postgres.DB) {
}
func (repository BiteRepository) 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
}
for _, model := range models {
biteModel, ok := model.(BiteModel)
if !ok {
tx.Rollback()
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("rollback error ", rollbackErr)
}
return fmt.Errorf("model of type %T, not %T", model, BiteModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.bite (header_id, ilk, urn, ink, art, iart, tab, nflip, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4::NUMERIC, $5::NUMERIC, $6::NUMERIC, $7::NUMERIC, $8::NUMERIC, $9, $10, $11)`,
headerID, biteModel.Ilk, biteModel.Urn, biteModel.Ink, biteModel.Art, biteModel.IArt, biteModel.Tab, biteModel.NFlip, biteModel.LogIndex, biteModel.TransactionIndex, biteModel.Raw,
)
if err != nil {
tx.Rollback()
return err
if execErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("rollback error ", rollbackErr)
}
return execErr
}
}
err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.BiteChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.BiteChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("rollback error ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()