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 return conn, err
} }
// IsErrNoRows returns true if the error indicates that no rows were
// returned from the database.
func IsErrNoRows(err error) bool { func IsErrNoRows(err error) bool {
return err == sql.ErrNoRows return err == sql.ErrNoRows
} }
// IsErrUniqueViolation returns true if the error indicates that a UNIQUE
// constraint violation has occured.
func IsErrUniqueViolation(err error) bool { func IsErrUniqueViolation(err error) bool {
// see if we can cast the error to a Database error type // see if we can cast the error to a Database error type
pqErr, ok := err.(*pq.Error) pqErr, ok := err.(*pq.Error)
@ -38,6 +42,8 @@ func IsErrUniqueViolation(err error) bool {
return true 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 { func IsErrForeignKeyViolation(err error) bool {
// see if we can cast the error to a Database error type // see if we can cast the error to a Database error type
pqErr, ok := err.(*pq.Error) pqErr, ok := err.(*pq.Error)