/pqerrors

Primary LanguageGoApache License 2.0Apache-2.0

pqerrors

Build Status GoDoc Supported Go Versions Go Report Card GitHub Release

pqerrors is a Go library that provides constants to make handling pq errors easier

Example Usage:

Error Classes

    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

    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 into errors_table.txt
  2. Run go generate
  3. Ensure that tests still pass: go test -v
  4. Commit the changes
  5. Open a PR