From 05351a8ef491d1e456280c45b98f53987f3d27ba Mon Sep 17 00:00:00 2001 From: yaoandrew <1547809+yaoandrew@users.noreply.github.com> Date: Wed, 19 Dec 2018 01:04:20 -0800 Subject: [PATCH] refactor error handling in dent --- pkg/transformers/dent/repository.go | 40 ++++++++++++++++++----------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/pkg/transformers/dent/repository.go b/pkg/transformers/dent/repository.go index 0ff5370b..00ca660f 100644 --- a/pkg/transformers/dent/repository.go +++ b/pkg/transformers/dent/repository.go @@ -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() }