Skip to content

feat: pgerr error inspection package and DB.InTx method#49

Merged
klaidliadon merged 2 commits into
masterfrom
feat/pgerr
May 13, 2026
Merged

feat: pgerr error inspection package and DB.InTx method#49
klaidliadon merged 2 commits into
masterfrom
feat/pgerr

Conversation

@klaidliadon
Copy link
Copy Markdown
Contributor

@klaidliadon klaidliadon commented May 13, 2026

Two additive changes for the dbkit lift happening downstream. Both are surfaces that consumers were about to re-implement in every project.


pgerr sub-package

Postgres error inspection helpers built on pgconn.PgError. Sits on top of github.com/jackc/pgerrcode for SQLSTATE constants; pgerr's own surface is just the inspection helpers that operate on error rather than raw code strings.

func IsErrorNoRows(err error) bool                  // pgx.ErrNoRows | sql.ErrNoRows
func IsUniqueViolation(err error) (string, bool)    // comma-ok: returns constraint name when SQLSTATE 23505
func IsFatal(err error) bool                        // class-based retry/alert gate

IsFatal treats SQLSTATE classes 00 (success), 01 (warning), 02 (no data), and 23 (integrity constraint violation) as non-fatal; every other class is fatal. Non-PgError errors return false. Class derivation uses the first two characters of the SQLSTATE code (not pgerrcode's per-class membership helpers), so any future code added within those classes is automatically covered.

DB.InTx(tx) *DB

func (d *DB) InTx(tx pgx.Tx) *DB

Returns a new *DB sharing Conn and SQL with the parent but routing queries through tx. Complements the existing TxQuery(tx) *Querier so callers can pass a tx-scoped *DB into anything that takes *pgkit.DB without rewriting parameter types. Pattern that every binary doing transactional flows ends up needing.

WithTx was considered for the name; rejected because pgkit.WithTx(ctx, tx) context.Context already exists at the package level and the cognitive overlap is bad. InTx reads cleanly at the call site.

Test plan

go test ./... excluding ./tests/... (DB-dependent integration tests) — 24 subtests across pgerr and pgkit root, all green.

go vet ./... clean, gofmt -l . clean, go build ./... clean.

Comment thread pgerr/errcode.go Outdated
Comment on lines +1 to +3
// This file is vendored from github.com/jackc/pgerrcode (MIT, Copyright (c) 2019 Jack Christensen).
// See LICENSE-pgerrcode in this directory for the full upstream license.
//
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's OK to add github.com/jackc/pgerrcode to go.mod dependencies. We rely on jack's pgx and other packages already.

Check this out - the repo is just this file: https://github.com/jackc/pgerrcode/blob/master/errcode.go

- IsErrorNoRows: fan-out over pgx.ErrNoRows and sql.ErrNoRows
- IsUniqueViolation: comma-ok, returns constraint name when matching SQLSTATE 23505
- IsFatal: class-based retry/alert classification (00/01/02/23 non-fatal)
- Depends on github.com/jackc/pgerrcode for SQLSTATE constants
- Returns a new *DB sharing Conn and SQL with the parent but routing
  queries through tx
- Complements the existing TxQuery(tx) *Querier so callers can pass a
  tx-scoped *DB into functions that take *pgkit.DB without rewriting
  parameter types
@klaidliadon klaidliadon changed the title feat(pgerr): add postgres error inspection package feat: pgerr error inspection package and DB.InTx method May 13, 2026
@klaidliadon klaidliadon merged commit 7f7dc8e into master May 13, 2026
1 check passed
@klaidliadon klaidliadon deleted the feat/pgerr branch May 13, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants