chore: initial commit
This commit is contained in:
49
internal/pkg/dberr/postgres.go
Normal file
49
internal/pkg/dberr/postgres.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package dberr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
const (
|
||||
SysUserAccountKey = "sys_users_account_key"
|
||||
SysRoleCodeKey = "sys_roles_code_key"
|
||||
PostSlugKey = "posts_slug_key"
|
||||
SysPermissionsCodeKey = "sys_permissions_code_key"
|
||||
SysApisMethodPathKey = "sys_apis_method_path_key"
|
||||
CategoryCodeKey = "categories_code_key"
|
||||
)
|
||||
|
||||
func MapRowsAffected(rows int64, err error, notFoundErr error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rows == 0 {
|
||||
return notFoundErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MapNoRows(err error, notFoundErr error) error {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return notFoundErr
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func MapUniqueViolation(err error, constraint string, uniqueErr error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) &&
|
||||
pgErr.Code == "23505" &&
|
||||
pgErr.ConstraintName == constraint {
|
||||
return uniqueErr
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user