refactor error handling in flip

This commit is contained in:
yaoandrew 2018-12-19 00:40:00 -08:00
parent 513513e93b
commit 843d30c88e

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 CatFileFlipRepository struct { type CatFileFlipRepository struct {
@ -26,32 +27,41 @@ type CatFileFlipRepository struct {
} }
func (repository CatFileFlipRepository) Create(headerID int64, models []interface{}) error { func (repository CatFileFlipRepository) 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 {
flip, ok := model.(CatFileFlipModel) flip, ok := model.(CatFileFlipModel)
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, CatFileFlipModel{}) return fmt.Errorf("model of type %T, not %T", model, CatFileFlipModel{})
} }
_, err = repository.db.Exec( _, execErr := repository.db.Exec(
`INSERT into maker.cat_file_flip (header_id, ilk, what, flip, tx_idx, log_idx, raw_log) `INSERT into maker.cat_file_flip (header_id, ilk, what, flip, tx_idx, log_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6, $7)`, VALUES($1, $2, $3, $4, $5, $6, $7)`,
headerID, flip.Ilk, flip.What, flip.Flip, flip.TransactionIndex, flip.LogIndex, flip.Raw, headerID, flip.Ilk, flip.What, flip.Flip, flip.TransactionIndex, flip.LogIndex, flip.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.CatFileFlipChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.CatFileFlipChecked)
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()
} }