jackc / pgconn

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improper error handling in pgconn.ConnectConfig

utemkin opened this issue · comments

Seems like there's improper error handling in pgconn.ConnectConfig

The condition in the following line

https://github.com/jackc/pgconn/blob/v1.5.0/pgconn.go#L142

should be inverse (!ok), otherwise it's impossible to access PgError to programmatically analyze cause of connection failure. Furthermore, current condition contradicts comment in line

https://github.com/jackc/pgconn/blob/v1.5.0/pgconn.go#L148

I'm not following you...

Line 142 is handling an error returned by connect. connect already has wrapped all error types except *PgError in *connectError. The reason *PgError is handles separately is to match the C library libpq in HA scenarios (multiple hosts specified). A *PgError terminates the attempt instead of trying the next host.

As far as accessing PgError programmatically, *connectError implements Unwrap. Use errors.Is or errors.As to test and access.

Sorry! I missed all those private xxxError implement Unwrap()