pqerrors is a Go library that provides constants to make handling pq errors easier
var err error // err from database/sql
if e, ok := err.(pq.Error); ok {
switch e.Code.Class() {
case pqerrcls.NoData:
// Handle error
case pqerrcls.IntegrityConstraintViolation:
// Handle error
default:
// Handle unexpected error
}
}
var err error // err from database/sql
if e, ok := err.(pq.Error); ok {
switch e.Code {
case pqerrcode.DataExceptionNullValueNotAllowed:
// Handle error
case pqerrcode.IntegrityConstraintViolationUniqueViolation:
// Handle error
default:
// Handle unexpected error
}
}
- Copy the table of Postgres errors into
errors_table.txt
- Run
go generate
- Ensure that tests still pass:
go test -v
- Commit the changes
- Open a PR