https://github.com/dhui/pqerrors
https://github.com/dhui/pqerrors
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/dhui/pqerrors
- Owner: dhui
- License: apache-2.0
- Created: 2018-09-09T05:33:04.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-08T05:52:31.000Z (about 7 years ago)
- Last Synced: 2025-02-10T09:13:05.747Z (11 months ago)
- Language: Go
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pqerrors
[](https://travis-ci.org/dhui/pqerrors)
[](https://godoc.org/github.com/dhui/pqerrors)

[](https://goreportcard.com/report/github.com/dhui/pqerrors)
[](https://github.com/dhui/pqerrors/releases)
pqerrors is a Go library that provides constants to make handling [pq](https://github.com/lib/pq) errors easier
## Example Usage:
### Error Classes
```golang
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
}
}
```
### Error Codes
```golang
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
}
}
```
## How to update error class and code constants:
1. Copy the [table of Postgres errors](https://www.postgresql.org/docs/current/static/errcodes-appendix.html) into `errors_table.txt`
1. Run `go generate`
1. Ensure that tests still pass: `go test -v`
1. Commit the changes
1. [Open a PR](https://github.com/dhui/pqerrors/pulls)