pgutil/errors.go
2024-10-29 19:05:09 +01:00

32 lines
489 B
Go

package pgutil
import (
"errors"
"github.com/lib/pq"
)
func ErrIsDuplicateKey(err error) bool {
return ErrHasCode(err, "23505")
}
func ErrIsForeignKey(err error) bool {
return ErrHasCode(err, "23503")
}
func ErrIsSerializationFaiilure(err error) bool {
return ErrHasCode(err, "40001")
}
func ErrHasCode(err error, code string) bool {
if err == nil {
return false
}
var pErr *pq.Error
if errors.As(err, &pErr) {
return pErr.Code == pq.ErrorCode(code)
}
return false
}