You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Jack Christensen edited this page May 13, 2023
·
2 revisions
Error Handling
pgx uses Go 1.13+ style error wrapping. That means that errors.Is() and errors.As() should be used instead of directly testing an error for equality or attempting a type assertion.
If an error is returned from PostgreSQL then underlying error type will be a *pgconn.PgError.
// Be sure the correct package is imported, this will not work if the old, standalone pgconn repo is imported instead.import"github.com/jackc/pgx/v5/pgconn"// ...err=conn.QueryRow(context.Background(), "select 1 +").Scan(&greeting)
iferr!=nil {
varpgErr*pgconn.PgErroriferrors.As(err, &pgErr) {
fmt.Println(pgErr.Message) // => syntax error at end of inputfmt.Println(pgErr.Code) // => 42601
}
}