Skip to content

Commit

Permalink
Return wrapped errors in mysql and postgres database Connect methods (w…
Browse files Browse the repository at this point in the history
  • Loading branch information
kkajla12 authored Apr 5, 2023
1 parent dcb474f commit 8f83cc2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func (ds *MySQL) Connect(ctx context.Context) error {

db, err = sqlx.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/?parseTime=true", ds.Config.Username, ds.Config.Password, ds.Config.Hostname))
if err != nil {
errors.Wrap(err, "Unable to establish connection to mysql. Shutting down server.")
return errors.Wrap(err, "Unable to establish connection to mysql. Shutting down server.")
}

err = db.PingContext(ctx)
if err != nil {
errors.Wrap(err, "Unable to ping mysql. Shutting down server.")
return errors.Wrap(err, "Unable to ping mysql. Shutting down server.")
}

if ds.Config.MaxIdleConnections != 0 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (ds *Postgres) Connect(ctx context.Context) error {

db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/?sslmode=%s", ds.Config.Username, ds.Config.Password, ds.Config.Hostname, ds.Config.SSLMode))
if err != nil {
errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
}

// create database if it does not already exist
Expand All @@ -56,12 +56,12 @@ func (ds *Postgres) Connect(ctx context.Context) error {
db.Close()
db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s", ds.Config.Username, ds.Config.Password, ds.Config.Hostname, ds.Config.Database, ds.Config.SSLMode))
if err != nil {
errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
}

err = db.PingContext(ctx)
if err != nil {
errors.Wrap(err, fmt.Sprintf("Unable to ping postgres database %s. Shutting down server.", ds.Config.Database))
return errors.Wrap(err, fmt.Sprintf("Unable to ping postgres database %s. Shutting down server.", ds.Config.Database))
}

if ds.Config.MaxIdleConnections != 0 {
Expand Down

0 comments on commit 8f83cc2

Please sign in to comment.