refactor error handling in drip, flap, flop

This commit is contained in:
yaoandrew 2018-12-19 01:26:33 -08:00
parent 05351a8ef4
commit 657aaccff3
7 changed files with 142 additions and 81 deletions

View File

@ -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 DripDripRepository struct {
@ -26,32 +27,41 @@ type DripDripRepository struct {
}
func (repository DripDripRepository) 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 {
dripDrip, ok := model.(DripDripModel)
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, DripDripModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.drip_drip (header_id, ilk, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5)`,
headerID, dripDrip.Ilk, dripDrip.LogIndex, dripDrip.TransactionIndex, dripDrip.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.DripDripChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripDripChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()
}

View File

@ -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 DripFileIlkRepository struct {
@ -26,34 +27,43 @@ type DripFileIlkRepository struct {
}
func (repository DripFileIlkRepository) 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 {
ilk, ok := model.(DripFileIlkModel)
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, DripFileIlkModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.drip_file_ilk (header_id, ilk, vow, tax, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`,
headerID, ilk.Ilk, ilk.Vow, ilk.Tax, ilk.LogIndex, ilk.TransactionIndex, ilk.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.DripFileIlkChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripFileIlkChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()

View File

@ -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 DripFileRepoRepository struct {
@ -26,34 +27,43 @@ type DripFileRepoRepository struct {
}
func (repository DripFileRepoRepository) 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 {
repo, ok := model.(DripFileRepoModel)
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, DripFileRepoModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.drip_file_repo (header_id, what, data, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4, $5, $6)`,
headerID, repo.What, repo.Data, repo.LogIndex, repo.TransactionIndex, repo.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.DripFileRepoChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripFileRepoChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()

View File

@ -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 DripFileVowRepository struct {
@ -26,33 +27,42 @@ type DripFileVowRepository struct {
}
func (repository DripFileVowRepository) 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 {
vow, ok := model.(DripFileVowModel)
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, DripFileVowModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.drip_file_vow (header_id, what, data, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6)`,
headerID, vow.What, vow.Data, vow.LogIndex, vow.TransactionIndex, vow.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.DripFileVowChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.DripFileVowChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()

View File

@ -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 FlapKickRepository struct {
@ -26,9 +27,9 @@ type FlapKickRepository struct {
}
func (repository *FlapKickRepository) 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 {
flapKickModel, ok := model.(FlapKickModel)
@ -36,21 +37,27 @@ func (repository *FlapKickRepository) Create(headerID int64, models []interface{
return fmt.Errorf("model of type %T, not %T", model, FlapKickModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.flap_kick (header_id, bid_id, lot, bid, gal, "end", tx_idx, log_idx, raw_log)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8, $9)`,
headerID, flapKickModel.BidId, flapKickModel.Lot, flapKickModel.Bid, flapKickModel.Gal, flapKickModel.End, flapKickModel.TransactionIndex, flapKickModel.LogIndex, flapKickModel.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.FlapKickChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.FlapKickChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()
}

View File

@ -16,6 +16,7 @@ package flip_kick
import (
"fmt"
"log"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
@ -27,9 +28,9 @@ type FlipKickRepository struct {
}
func (repository FlipKickRepository) 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 {
flipKickModel, ok := model.(FlipKickModel)
@ -37,20 +38,26 @@ func (repository FlipKickRepository) Create(headerID int64, models []interface{}
return fmt.Errorf("model of type %T, not %T", model, FlipKickModel{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.flip_kick (header_id, bid_id, lot, bid, gal, "end", urn, tab, tx_idx, log_idx, raw_log)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8::NUMERIC, $9, $10, $11)`,
headerID, flipKickModel.BidId, flipKickModel.Lot, flipKickModel.Bid, flipKickModel.Gal, flipKickModel.End, flipKickModel.Urn, flipKickModel.Tab, flipKickModel.TransactionIndex, flipKickModel.LogIndex, flipKickModel.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.FlipKickChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.FlipKickChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()
}

View File

@ -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 FlopKickRepository struct {
@ -26,9 +27,9 @@ type FlopKickRepository struct {
}
func (repository FlopKickRepository) 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 _, flopKick := range models {
flopKickModel, ok := flopKick.(Model)
@ -37,21 +38,27 @@ func (repository FlopKickRepository) Create(headerID int64, models []interface{}
return fmt.Errorf("model of type %T, not %T", flopKick, Model{})
}
_, err = tx.Exec(
_, execErr := tx.Exec(
`INSERT into maker.flop_kick (header_id, bid_id, lot, bid, gal, "end", tx_idx, log_idx, raw_log)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8, $9)`,
headerID, flopKickModel.BidId, flopKickModel.Lot, flopKickModel.Bid, flopKickModel.Gal, flopKickModel.End, flopKickModel.TransactionIndex, flopKickModel.LogIndex, flopKickModel.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.FlopKickChecked)
if err != nil {
tx.Rollback()
return err
checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.FlopKickChecked)
if checkHeaderErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Println("failed to rollback ", rollbackErr)
}
return checkHeaderErr
}
return tx.Commit()