go/pgutil/errors.go

32 lines
488 B
Go
Raw Normal View History

2024-11-11 05:36:55 +00:00
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")
}
2024-11-17 14:18:05 +00:00
func ErrIsSerializationFailure(err error) bool {
2024-11-11 05:36:55 +00:00
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
}