From fb84192eef75c909d031aa867d407f1b7797e9db Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 19:57:15 -0500 Subject: Add more wrapped errors --- internal/storage/db.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'internal/storage/db.go') diff --git a/internal/storage/db.go b/internal/storage/db.go index 9032dc3..e329e71 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -15,16 +15,15 @@ import ( func DBConnect(driver string, connectionParams string) (*sql.DB, error) { db, err := sql.Open(driver, connectionParams) if err != nil { - return nil, err + return nil, fmt.Errorf("invalid database connection parameters: %w", err) } - // Note: `sql.Open` only validates the connection string we provided - // it is sane. It doesn't actually open up a connection to the database. - // For that, we ping the database to ensure the connection string is fully - // valid. + // Note: `sql.Open` only validates the connection string we provided is sane. + // It doesn't actually open up a connection to the database. For that, we ping + // the database to ensure the connection string is fully valid. err = db.Ping() if err != nil { - return nil, err + return nil, fmt.Errorf("unable to ping database: %w", err) } return db, nil -- cgit v1.2.3 From ed987d2d21e4063fcdb83e86d9f87b08f8e2165a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 16:03:14 -0500 Subject: Add better error handling for db connect --- internal/storage/db.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'internal/storage/db.go') diff --git a/internal/storage/db.go b/internal/storage/db.go index e329e71..26d3b3b 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -2,12 +2,18 @@ package storage import ( "database/sql" + "errors" "fmt" "github.com/eugenetriguba/bolt/internal/configloader" _ "github.com/lib/pq" ) +var ( + ErrInvalidConnectionString = errors.New("invalid database connection parameters provided") + ErrUnableToConnect = errors.New("unable to open connection to database") +) + // DBConnect establishes a connection to the database using the driver // and connection information. // @@ -15,7 +21,7 @@ import ( func DBConnect(driver string, connectionParams string) (*sql.DB, error) { db, err := sql.Open(driver, connectionParams) if err != nil { - return nil, fmt.Errorf("invalid database connection parameters: %w", err) + return nil, fmt.Errorf("%w: %v", ErrInvalidConnectionString, err) } // Note: `sql.Open` only validates the connection string we provided is sane. @@ -23,7 +29,7 @@ func DBConnect(driver string, connectionParams string) (*sql.DB, error) { // the database to ensure the connection string is fully valid. err = db.Ping() if err != nil { - return nil, fmt.Errorf("unable to ping database: %w", err) + return nil, fmt.Errorf("%w: %v", ErrUnableToConnect, err) } return db, nil -- cgit v1.2.3 From 46f7f49ebeb7f4e8fa1f395805a0b14baee5e5bc Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 16:08:04 -0500 Subject: Tweak error and doc for storage module --- internal/storage/db.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'internal/storage/db.go') diff --git a/internal/storage/db.go b/internal/storage/db.go index 26d3b3b..02683b4 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -10,18 +10,22 @@ import ( ) var ( - ErrInvalidConnectionString = errors.New("invalid database connection parameters provided") - ErrUnableToConnect = errors.New("unable to open connection to database") + ErrMalformedConnectionString = errors.New("malformed database connection parameters provided") + ErrUnableToConnect = errors.New("unable to open connection to database") ) // DBConnect establishes a connection to the database using the driver -// and connection information. +// and connection information. Only "postgres" is supported as the driver. // -// Note that only "postgres" is supported as the driver right now. +// The following errors may be returned: +// - ErrMalformedConnectionString: The provided connection parameters are +// not in a valid format. +// - ErrUnableToConnect: Unable to make a connection to the database with +// the provided connection parameters. func DBConnect(driver string, connectionParams string) (*sql.DB, error) { db, err := sql.Open(driver, connectionParams) if err != nil { - return nil, fmt.Errorf("%w: %v", ErrInvalidConnectionString, err) + return nil, fmt.Errorf("%w: %v", ErrMalformedConnectionString, err) } // Note: `sql.Open` only validates the connection string we provided is sane. -- cgit v1.2.3 From bf1d3b3e1fc42132678b244e479839416a03a6ce Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 29 Feb 2024 19:53:20 -0500 Subject: Add service tests --- internal/storage/db.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'internal/storage/db.go') diff --git a/internal/storage/db.go b/internal/storage/db.go index 02683b4..a9e35af 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -10,8 +10,10 @@ import ( ) var ( - ErrMalformedConnectionString = errors.New("malformed database connection parameters provided") - ErrUnableToConnect = errors.New("unable to open connection to database") + ErrMalformedConnectionString = errors.New( + "malformed database connection parameters provided", + ) + ErrUnableToConnect = errors.New("unable to open connection to database") ) // DBConnect establishes a connection to the database using the driver -- cgit v1.2.3