92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package db
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/lib/pq"
|
|
)
|
|
|
|
// Errors returned from this package may be tested against these errors
|
|
// with errors.Is.
|
|
var (
|
|
// ErrInvalidPublicId indicates an invalid PublicId.
|
|
ErrInvalidPublicId = errors.New("invalid publicId")
|
|
|
|
// ErrInvalidParameter is returned by create and update methods if
|
|
// an attribute on a struct contains illegal or invalid values.
|
|
ErrInvalidParameter = errors.New("invalid parameter")
|
|
|
|
// ErrInvalidFieldMask is returned by update methods if the field mask
|
|
// contains unknown fields or fields that cannot be updated.
|
|
ErrInvalidFieldMask = errors.New("invalid field mask")
|
|
|
|
// ErrEmptyFieldMask is returned by update methods if the field mask is
|
|
// empty.
|
|
ErrEmptyFieldMask = errors.New("empty field mask")
|
|
|
|
// ErrNotUnique is returned by create and update methods when a write
|
|
// to the repository resulted in a unique constraint violation.
|
|
ErrNotUnique = errors.New("unique constraint violation")
|
|
|
|
// ErrRecordNotFound returns a "record not found" error and it only occurs
|
|
// when attempting to read from the database into struct.
|
|
// When reading into a slice it won't return this error.
|
|
ErrRecordNotFound = errors.New("record not found")
|
|
|
|
// ErrMultipleRecords is returned by update and delete methods when a
|
|
// write to the repository would result in more than one record being
|
|
// changed resulting in the transaction being rolled back.
|
|
ErrMultipleRecords = errors.New("multiple records")
|
|
)
|
|
|
|
// IsUniqueError returns a boolean indicating whether the error is known to
|
|
// report a unique constraint violation.
|
|
func IsUniqueError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
|
|
var pqError *pq.Error
|
|
if errors.As(err, &pqError) {
|
|
if pqError.Code.Name() == "unique_violation" {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsCheckConstraintError returns a boolean indicating whether the error is
|
|
// known to report a check constraint violation.
|
|
func IsCheckConstraintError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
|
|
var pqError *pq.Error
|
|
if errors.As(err, &pqError) {
|
|
if pqError.Code.Name() == "check_violation" {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsNotNullError returns a boolean indicating whether the error is known
|
|
// to report a not-null constraint violation.
|
|
func IsNotNullError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
|
|
var pqError *pq.Error
|
|
if errors.As(err, &pqError) {
|
|
if pqError.Code.Name() == "not_null_violation" {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|