feat: pgerr error inspection package and DB.InTx method#49
Merged
Conversation
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. | ||
| // |
Member
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two additive changes for the dbkit lift happening downstream. Both are surfaces that consumers were about to re-implement in every project.
pgerrsub-packagePostgres error inspection helpers built on
pgconn.PgError. Sits on top ofgithub.com/jackc/pgerrcodefor SQLSTATE constants; pgerr's own surface is just the inspection helpers that operate onerrorrather than raw code strings.IsFataltreats SQLSTATE classes 00 (success), 01 (warning), 02 (no data), and 23 (integrity constraint violation) as non-fatal; every other class is fatal. Non-PgErrorerrors 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) *DBReturns a new
*DBsharing Conn and SQL with the parent but routing queries throughtx. Complements the existingTxQuery(tx) *Querierso callers can pass a tx-scoped*DBinto anything that takes*pgkit.DBwithout rewriting parameter types. Pattern that every binary doing transactional flows ends up needing.WithTxwas considered for the name; rejected becausepgkit.WithTx(ctx, tx) context.Contextalready exists at the package level and the cognitive overlap is bad.InTxreads cleanly at the call site.Test plan
go test ./...excluding./tests/...(DB-dependent integration tests) — 24 subtests acrosspgerrandpgkitroot, all green.go vet ./...clean,gofmt -l .clean,go build ./...clean.