Skip to content

Add pqerror package#1275

Open
arp242 wants to merge 1 commit intomasterfrom
pqerror
Open

Add pqerror package#1275
arp242 wants to merge 1 commit intomasterfrom
pqerror

Conversation

@arp242
Copy link
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
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
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