refactor error handling in flip_deal

This commit is contained in:
yaoandrew 2018-12-19 00:57:41 -08:00
parent f655997fde
commit 08ac7f18a5

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 DealRepository struct { type DealRepository struct {
@ -26,33 +27,42 @@ type DealRepository struct {
} }
func (repository DealRepository) Create(headerID int64, models []interface{}) error { func (repository DealRepository) 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 {
dealModel, ok := model.(DealModel) dealModel, ok := model.(DealModel)
if !ok { 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, DealModel{}) return fmt.Errorf("model of type %T, not %T", model, DealModel{})
} }
_, err = tx.Exec( _, execErr := tx.Exec(
`INSERT into maker.deal (header_id, bid_id, contract_address, log_idx, tx_idx, raw_log) `INSERT into maker.deal (header_id, bid_id, contract_address, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6)`, VALUES($1, $2, $3, $4, $5, $6)`,
headerID, dealModel.BidId, dealModel.ContractAddress, dealModel.LogIndex, dealModel.TransactionIndex, dealModel.Raw, headerID, dealModel.BidId, dealModel.ContractAddress, dealModel.LogIndex, dealModel.TransactionIndex, dealModel.Raw,
) )
if err != nil { if execErr != nil {
tx.Rollback() rollbackErr := tx.Rollback()
return err if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return execErr
} }
} }
err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DealChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DealChecked)
if err != nil { if checkHeaderErr != nil {
tx.Rollback() rollbackErr := tx.Rollback()
return err if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
} }
return tx.Commit() return tx.Commit()
} }