Skip to content

Add pqerror package#1275

Merged
arp242 merged 3 commits intomasterfrom
pqerror
Mar 18, 2026
Merged

Add pqerror package#1275
arp242 merged 3 commits intomasterfrom
pqerror

Conversation

@arp242
Copy link
Copy Markdown
Collaborator

@arp242 arp242 commented Mar 10, 2026

Add a pqerror package with a list of error codes. Typical usage might be something like:

pqErr, ok := errors.AsType[*pq.Error](err)
if ok && pqErr == pqerror.UniqueViolation {
	return fmt.Errorf("user already exists")
}

To make this a bit more convenient, it also adds a pqerror.As() function:

pqErr := pqerror.As(err, pqerror.UniqueViolation)
if pqErr != nil {
	log.Fatalf("email %q already exsts", email)
}
if err != nil {
	return err
}

This also moves most of the error stuff to the pqerror package, with type aliases in the pq package so it won't break anything. Has to export Error.Query to make this work, which is okay. Keep most of the tests in the main pq package for now, to ensure this works.

Fixes #492

@arp242 arp242 mentioned this pull request Mar 10, 2026
@arp242 arp242 force-pushed the pqerror branch 3 times, most recently from 3758a44 to fe62098 Compare March 17, 2026 22:46
arp242 added 2 commits March 17, 2026 23:37
Add a pqerror package with a list of error codes. Typical usage might be
something like:

	pqErr, ok := errors.AsType[*pq.Error](err)
	if ok && pqErr == pqerror.UniqueViolation {
		return fmt.Errorf("user already exists")
	}

To make this a bit more convenient, it also adds a pqerror.As()
function:

	pqErr := pqerror.As(err, pqerror.UniqueViolation)
	if pqErr != nil {
		log.Fatalf("email %q already exsts", email)
	}
	if err != nil {
		return err
	}

This also moves most of the error stuff to the pqerror package, with
type aliases in the pq package so it won't break anything. Has to export
Error.Query to make this work, which is okay. Keep most of the tests in
the main pq package for now, to ensure this works.

Fixes #492
Still move pq.ErrorCode and pq.ErrorClass to pqerror.Code and
pqerror.Class, so we can use it in pqerror.

Also move the error severity values here. Seems to make sense.
@arp242 arp242 merged commit 0962458 into master Mar 18, 2026
13 checks passed
@arp242 arp242 deleted the pqerror branch March 18, 2026 01:04
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.

pg error code helpers

1 participant