From 843d30c88e7eeb9cd2f892316009a5ec279755a4 Mon Sep 17 00:00:00 2001 From: yaoandrew <1547809+yaoandrew@users.noreply.github.com> Date: Wed, 19 Dec 2018 00:40:00 -0800 Subject: [PATCH] refactor error handling in flip --- pkg/transformers/cat_file/flip/repository.go | 34 +++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/transformers/cat_file/flip/repository.go b/pkg/transformers/cat_file/flip/repository.go index a07ca207..5d305226 100644 --- a/pkg/transformers/cat_file/flip/repository.go +++ b/pkg/transformers/cat_file/flip/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 CatFileFlipRepository struct { @@ -26,32 +27,41 @@ type CatFileFlipRepository struct { } func (repository CatFileFlipRepository) 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 { flip, ok := model.(CatFileFlipModel) 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{}) } - _, 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) VALUES($1, $2, $3, $4, $5, $6, $7)`, headerID, flip.Ilk, flip.What, flip.Flip, flip.TransactionIndex, flip.LogIndex, flip.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.CatFileFlipChecked) - if err != nil { - tx.Rollback() - return err + checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.CatFileFlipChecked) + if checkHeaderErr != nil { + rollbackErr := tx.Rollback() + if rollbackErr != nil { + log.Println("failed to rollback ", rollbackErr) + } + return checkHeaderErr } return tx.Commit() }