diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-25 16:03:14 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-25 16:03:14 -0500 |
| commit | ed987d2d21e4063fcdb83e86d9f87b08f8e2165a (patch) | |
| tree | 03eb2196ce2fa2e0af11c7ea6dabbcafe749430d /internal/storage | |
| parent | Add error handling to unhandled errors during migration apply/revert (diff) | |
| download | cli-ed987d2d21e4063fcdb83e86d9f87b08f8e2165a.zip | |
Add better error handling for db connect
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/db.go | 10 | ||||
| -rw-r--r-- | internal/storage/db_test.go | 8 |
2 files changed, 13 insertions, 5 deletions
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 diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index 61a9bf4..c871c47 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -10,7 +10,7 @@ import ( "github.com/eugenetriguba/checkmate/assert" ) -func TestDBConnectWithPostgres(t *testing.T) { +func TestDBConnect_Success(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "postgres") conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) @@ -21,15 +21,17 @@ func TestDBConnectWithPostgres(t *testing.T) { assert.Nil(t, err) } -func TestDBConnectMalformedConnectionString(t *testing.T) { +func TestDBConnect_BadConnectionString(t *testing.T) { _, err := storage.DBConnect("postgres", "pizza=123") + assert.ErrorIs(t, err, storage.ErrUnableToConnect) assert.ErrorContains(t, err, "connection refused") } -func TestDBConnectUnsupportedDriver(t *testing.T) { +func TestDBConnect_UnsupportedDriver(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "redis") _, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + assert.ErrorIs(t, err, storage.ErrInvalidConnectionString) assert.ErrorContains(t, err, `unknown driver "redis"`) } |