Document database error helpers

This commit is contained in:
paul 2019-08-22 00:48:48 +02:00
parent 63b1cf1c3f
commit ac5f74988f

View file

@ -18,10 +18,14 @@ func New(dsn string) (*sqlx.DB, error) {
return conn, err
}
// IsErrNoRows returns true if the error indicates that no rows were
// returned from the database.
func IsErrNoRows(err error) bool {
return err == sql.ErrNoRows
}
// IsErrUniqueViolation returns true if the error indicates that a UNIQUE
// constraint violation has occured.
func IsErrUniqueViolation(err error) bool {
// see if we can cast the error to a Database error type
pqErr, ok := err.(*pq.Error)
@ -38,6 +42,8 @@ func IsErrUniqueViolation(err error) bool {
return true
}
// IsErrForeignKeyViolation returns true if the error indicates that a
// FOREIGN KEY constraint has been violated due to the requested changes.
func IsErrForeignKeyViolation(err error) bool {
// see if we can cast the error to a Database error type
pqErr, ok := err.(*pq.Error)