@@ -5,13 +5,37 @@ import (
55 "fmt"
66 "strconv"
77
8- "github.com/cybertec-postgresql/pgcov/internal/errors"
98 "github.com/cybertec-postgresql/pgcov/pkg/types"
109 "github.com/jackc/pgx/v5/pgxpool"
1110)
1211
1312const applicationName = "pgcov"
1413
14+ // ConnectionError represents PostgreSQL connection failure
15+ type ConnectionError struct {
16+ Host string
17+ Port int
18+ Message string
19+ Suggestion string
20+ }
21+
22+ func (e * ConnectionError ) Error () string {
23+ msg := fmt .Sprintf ("failed to connect to %s:%d: %s" , e .Host , e .Port , e .Message )
24+ if e .Suggestion != "" {
25+ msg += fmt .Sprintf ("\n Suggestion: %s" , e .Suggestion )
26+ }
27+ return msg
28+ }
29+
30+ // NewConnectionError creates a new ConnectionError
31+ func NewConnectionError (host string , port int , message string ) * ConnectionError {
32+ return & ConnectionError {
33+ Host : host ,
34+ Port : port ,
35+ Message : message ,
36+ }
37+ }
38+
1539// Pool wraps pgxpool.Pool with additional functionality
1640type Pool struct {
1741 * pgxpool.Pool
@@ -26,7 +50,7 @@ func NewPool(ctx context.Context, config *types.Config) (*Pool, error) {
2650 // Configure pool
2751 poolConfig , err := pgxpool .ParseConfig (connString )
2852 if err != nil {
29- return nil , & errors. ConnectionError {
53+ return nil , & ConnectionError {
3054 Message : fmt .Sprintf ("invalid connection configuration: %v" , err ),
3155 Suggestion : "Check your PostgreSQL connection string format. Use URI format (postgresql://user:pass@host:port/db) or key=value format (host=localhost port=5432 ...)" ,
3256 }
@@ -45,7 +69,7 @@ func NewPool(ctx context.Context, config *types.Config) (*Pool, error) {
4569 // Create pool
4670 pool , err := pgxpool .NewWithConfig (ctx , poolConfig )
4771 if err != nil {
48- return nil , & errors. ConnectionError {
72+ return nil , & ConnectionError {
4973 Message : fmt .Sprintf ("failed to create connection pool: %v" , err ),
5074 Suggestion : "Verify PostgreSQL is running and accessible with the provided connection string" ,
5175 }
@@ -56,23 +80,23 @@ func NewPool(ctx context.Context, config *types.Config) (*Pool, error) {
5680 err = pool .QueryRow (ctx , "SHOW server_version_num" ).Scan (& versionStr )
5781 if err != nil {
5882 pool .Close ()
59- return nil , & errors. ConnectionError {
83+ return nil , & ConnectionError {
6084 Message : fmt .Sprintf ("failed to query PostgreSQL version: %v" , err ),
6185 }
6286 }
6387
6488 version , err := strconv .Atoi (versionStr )
6589 if err != nil {
6690 pool .Close ()
67- return nil , & errors. ConnectionError {
91+ return nil , & ConnectionError {
6892 Message : fmt .Sprintf ("failed to parse PostgreSQL version '%s': %v" , versionStr , err ),
6993 }
7094 }
7195
7296 // PostgreSQL 13+ required (version 130000+)
7397 if version < 130000 {
7498 pool .Close ()
75- return nil , & errors. ConnectionError {
99+ return nil , & ConnectionError {
76100 Message : fmt .Sprintf ("PostgreSQL version %d is not supported (need 13+)" , version / 10000 ),
77101 Suggestion : "Upgrade to PostgreSQL 13 or later" ,
78102 }
0 commit comments