refactor error handling in frob, pit, price, tend

This commit is contained in:
yaoandrew 2018-12-19 01:37:44 -08:00
parent 657aaccff3
commit 525fada3d7
5 changed files with 113 additions and 63 deletions

View File

@ -16,6 +16,7 @@ package frob
import ( import (
"fmt" "fmt"
"log"
"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"
@ -27,29 +28,38 @@ type FrobRepository struct {
} }
func (repository FrobRepository) Create(headerID int64, models []interface{}) error { func (repository FrobRepository) 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 {
frobModel, ok := model.(FrobModel) frobModel, ok := model.(FrobModel)
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, FrobModel{}) return fmt.Errorf("model of type %T, not %T", model, FrobModel{})
} }
_, err = tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, log_idx, tx_idx) _, execErr := tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, log_idx, tx_idx)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10, $11)`, VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10, $11)`,
headerID, frobModel.Art, frobModel.Dart, frobModel.Dink, frobModel.IArt, frobModel.Ilk, frobModel.Ink, frobModel.Urn, frobModel.Raw, frobModel.LogIndex, frobModel.TransactionIndex) headerID, frobModel.Art, frobModel.Dart, frobModel.Dink, frobModel.IArt, frobModel.Ilk, frobModel.Ink, frobModel.Urn, frobModel.Raw, frobModel.LogIndex, frobModel.TransactionIndex)
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.FrobChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.FrobChecked)
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()
} }

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 PitFileDebtCeilingRepository struct { type PitFileDebtCeilingRepository struct {
@ -26,34 +27,43 @@ type PitFileDebtCeilingRepository struct {
} }
func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []interface{}) error { func (repository PitFileDebtCeilingRepository) 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 {
pitFileDC, ok := model.(PitFileDebtCeilingModel) pitFileDC, ok := model.(PitFileDebtCeilingModel)
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, PitFileDebtCeilingModel{}) return fmt.Errorf("model of type %T, not %T", model, PitFileDebtCeilingModel{})
} }
_, err = tx.Exec( _, execErr := tx.Exec(
`INSERT into maker.pit_file_debt_ceiling (header_id, what, data, log_idx, tx_idx, raw_log) `INSERT into maker.pit_file_debt_ceiling (header_id, what, data, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4, $5, $6)`, VALUES($1, $2, $3::NUMERIC, $4, $5, $6)`,
headerID, pitFileDC.What, pitFileDC.Data, pitFileDC.LogIndex, pitFileDC.TransactionIndex, pitFileDC.Raw, headerID, pitFileDC.What, pitFileDC.Data, pitFileDC.LogIndex, pitFileDC.TransactionIndex, pitFileDC.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.PitFileDebtCeilingChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.PitFileDebtCeilingChecked)
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()

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 PitFileIlkRepository struct { type PitFileIlkRepository struct {
@ -26,33 +27,42 @@ type PitFileIlkRepository struct {
} }
func (repository PitFileIlkRepository) Create(headerID int64, models []interface{}) error { func (repository PitFileIlkRepository) 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 {
pitFileIlk, ok := model.(PitFileIlkModel) pitFileIlk, ok := model.(PitFileIlkModel)
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, PitFileIlkModel{}) return fmt.Errorf("model of type %T, not %T", model, PitFileIlkModel{})
} }
_, err = tx.Exec( _, execErr := tx.Exec(
`INSERT into maker.pit_file_ilk (header_id, ilk, what, data, log_idx, tx_idx, raw_log) `INSERT into maker.pit_file_ilk (header_id, ilk, what, data, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`,
headerID, pitFileIlk.Ilk, pitFileIlk.What, pitFileIlk.Data, pitFileIlk.LogIndex, pitFileIlk.TransactionIndex, pitFileIlk.Raw, headerID, pitFileIlk.Ilk, pitFileIlk.What, pitFileIlk.Data, pitFileIlk.LogIndex, pitFileIlk.TransactionIndex, pitFileIlk.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.PitFileIlkChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.PitFileIlkChecked)
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()
} }

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 PriceFeedRepository struct { type PriceFeedRepository struct {
@ -26,29 +27,38 @@ type PriceFeedRepository struct {
} }
func (repository PriceFeedRepository) Create(headerID int64, models []interface{}) error { func (repository PriceFeedRepository) 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 {
priceUpdate, ok := model.(PriceFeedModel) priceUpdate, ok := model.(PriceFeedModel)
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, PriceFeedModel{}) return fmt.Errorf("model of type %T, not %T", model, PriceFeedModel{})
} }
_, err = tx.Exec(`INSERT INTO maker.price_feeds (block_number, header_id, medianizer_address, usd_value, log_idx, tx_idx, raw_log) _, execErr := tx.Exec(`INSERT INTO maker.price_feeds (block_number, header_id, medianizer_address, usd_value, log_idx, tx_idx, raw_log)
VALUES ($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, priceUpdate.BlockNumber, headerID, priceUpdate.MedianizerAddress, priceUpdate.UsdValue, priceUpdate.LogIndex, priceUpdate.TransactionIndex, priceUpdate.Raw) VALUES ($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, priceUpdate.BlockNumber, headerID, priceUpdate.MedianizerAddress, priceUpdate.UsdValue, priceUpdate.LogIndex, priceUpdate.TransactionIndex, priceUpdate.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.PriceFeedsChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.PriceFeedsChecked)
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()
} }

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 TendRepository struct { type TendRepository struct {
@ -26,39 +27,48 @@ type TendRepository struct {
} }
func (repository TendRepository) Create(headerID int64, models []interface{}) error { func (repository TendRepository) 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
} }
tic, err := shared.GetTicInTx(headerID, tx) tic, getTicErr := shared.GetTicInTx(headerID, tx)
if err != nil { if getTicErr != nil {
return err return getTicErr
} }
for _, model := range models { for _, model := range models {
tend, ok := model.(TendModel) tend, ok := model.(TendModel)
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, TendModel{}) return fmt.Errorf("model of type %T, not %T", model, TendModel{})
} }
_, err = tx.Exec( _, execErr := tx.Exec(
`INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log) `INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8, $9)`, VALUES($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8, $9)`,
headerID, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tic, tend.LogIndex, tend.TransactionIndex, tend.Raw, headerID, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tic, tend.LogIndex, tend.TransactionIndex, tend.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.TendChecked) checkHeaderErr := shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.TendChecked)
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()
} }