jackc / pgconn

MIT License
182 stars 87 forks source link

Can "connect error" and "ssl error" be part of the public API? #118

Open rafiss opened 2 years ago

rafiss commented 2 years ago

I am working on a CLI tool that uses this library.

Currently, pgconn returns an untyped error for "server refused TLS." See https://github.com/jackc/pgconn/blob/7ddbd74d5e5a52cd24f750cacfc9be91d4f49f76/pgconn.go#L379

This causes some friction, as I need conditional logic based on this error. The only way now is with string matching, which is not guaranteed to be stable across releases.

Can this instead be a public error const, like ErrTLSNotSupported?

Similarly, I need conditional logic for connection errors, but that error is private now. See https://github.com/jackc/pgconn/blob/7ddbd74d5e5a52cd24f750cacfc9be91d4f49f76/errors.go#L60

Can that struct be made publicly visible, similar to PgError?


If you agree, I don't mind working on the PR.

jackc commented 2 years ago

I think it is reasonable to make it easier to programmatically handle specific errors.

But error constants and error types become part of the public interface and a tricky part at that. It's much less obvious that there has been a breaking change when a function signature changes than when a returned error changes. This is why I default to untyped / unexported errors.

That said, I think the two changes you suggest are reasonable and may even be optimal, but I'd like you to also consider whether an error interface (like net.Error) or type testing functions (like pgconn.SafeToRetry) would be a better interface. Your original suggestion is probably best but please consider alternatives as well.