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/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"log"
) )
type BiteRepository struct { type BiteRepository struct {
@ -30,32 +31,41 @@ func (repository *BiteRepository) SetDB(db *postgres.DB) {
} }
func (repository BiteRepository) Create(headerID int64, models []interface{}) error { func (repository BiteRepository) Create(headerID int64, models []interface{}) error {
tx, err := repository.db.Begin() tx, dBaseErr := repository.db.Begin()
if err != nil { if dBaseErr != nil {
return err return dBaseErr
} }
for _, model := range models { for _, model := range models {
biteModel, ok := model.(BiteModel) biteModel, ok := model.(BiteModel)
if !ok { 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{}) 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) `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)`, 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, headerID, biteModel.Ilk, biteModel.Urn, biteModel.Ink, biteModel.Art, biteModel.IArt, biteModel.Tab, biteModel.NFlip, biteModel.LogIndex, biteModel.TransactionIndex, biteModel.Raw,
) )
if err != nil { if execErr != nil {
tx.Rollback() rollbackErr := tx.Rollback()
return err if rollbackErr != nil {
log.Println("rollback error ", rollbackErr)
}
return execErr
} }
} }
err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.BiteChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.BiteChecked)
if err != nil { if checkHeaderErr != nil {
tx.Rollback() rollbackErr := tx.Rollback()
return err if rollbackErr != nil {
log.Println("rollback error ", rollbackErr)
}
return checkHeaderErr
} }
return tx.Commit() return tx.Commit()