forked from cerc-io/ipld-eth-server
Remove fatal from pkg
* Functions in pkg should return errors * If aborting is desired, that behavior should be left to the consumer
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
@@ -50,8 +49,9 @@ func parseConfigFile(filePath string) (*Config, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if _, err := toml.DecodeFile(filePath, &cfg); err != nil {
|
||||
log.Fatal(err)
|
||||
_, err := toml.DecodeFile(filePath, &cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, err
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
"context"
|
||||
|
||||
@@ -19,16 +18,17 @@ type Postgres struct {
|
||||
}
|
||||
|
||||
var (
|
||||
ErrDBInsertFailed = errors.New("postgres: insert failed")
|
||||
ErrDBInsertFailed = errors.New("postgres: insert failed")
|
||||
ErrDBConnectionFailed = errors.New("postgres: db connection failed")
|
||||
)
|
||||
|
||||
func NewPostgres(databaseConfig config.Database) Postgres {
|
||||
func NewPostgres(databaseConfig config.Database) (Postgres, error) {
|
||||
connectString := config.DbConnectionString(databaseConfig)
|
||||
db, err := sqlx.Connect("postgres", connectString)
|
||||
if err != nil {
|
||||
log.Fatalf("Error connecting to DB: %v\n", err)
|
||||
return Postgres{}, ErrDBConnectionFailed
|
||||
}
|
||||
return Postgres{Db: db}
|
||||
return Postgres{Db: db}, nil
|
||||
}
|
||||
|
||||
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
|
||||
@@ -42,11 +42,8 @@ func (repository Postgres) CreateWatchedContract(contract core.WatchedContract)
|
||||
|
||||
func (repository Postgres) IsWatchedContract(contractHash string) bool {
|
||||
var exists bool
|
||||
err := repository.Db.QueryRow(
|
||||
repository.Db.QueryRow(
|
||||
`SELECT exists(SELECT 1 FROM watched_contracts WHERE contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
log.Fatalf("error checking if row exists %v", err)
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ var _ = Describe("Postgres repository", func() {
|
||||
|
||||
testing.AssertRepositoryBehavior(func() repositories.Repository {
|
||||
cfg, _ := config.NewConfig("private")
|
||||
repository := repositories.NewPostgres(cfg.Database)
|
||||
repository, _ := repositories.NewPostgres(cfg.Database)
|
||||
testing.ClearData(repository)
|
||||
return repository
|
||||
})
|
||||
@@ -40,7 +40,7 @@ var _ = Describe("Postgres repository", func() {
|
||||
Transactions: []core.Transaction{},
|
||||
}
|
||||
cfg, _ := config.NewConfig("private")
|
||||
repository := repositories.NewPostgres(cfg.Database)
|
||||
repository, _ := repositories.NewPostgres(cfg.Database)
|
||||
|
||||
err := repository.CreateBlock(badBlock)
|
||||
savedBlock := repository.FindBlockByNumber(123)
|
||||
@@ -49,6 +49,12 @@ var _ = Describe("Postgres repository", func() {
|
||||
Expect(savedBlock).To(BeNil())
|
||||
})
|
||||
|
||||
It("throws error when can't connect to the database", func() {
|
||||
invalidDatabase := config.Database{}
|
||||
_, err := repositories.NewPostgres(invalidDatabase)
|
||||
Expect(err).To(Equal(repositories.ErrDBConnectionFailed))
|
||||
})
|
||||
|
||||
It("does not commit block or transactions if transaction is invalid", func() {
|
||||
//badHash violates db To field length
|
||||
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
||||
@@ -58,7 +64,7 @@ var _ = Describe("Postgres repository", func() {
|
||||
Transactions: []core.Transaction{badTransaction},
|
||||
}
|
||||
cfg, _ := config.NewConfig("private")
|
||||
repository := repositories.NewPostgres(cfg.Database)
|
||||
repository, _ := repositories.NewPostgres(cfg.Database)
|
||||
|
||||
err := repository.CreateBlock(block)
|
||||
savedBlock := repository.FindBlockByNumber(123)
|
||||
|
||||
Reference in New Issue
Block a user