fix: 配送点编码重复时返回中文提醒

This commit is contained in:
czl231
2026-08-10 12:33:30 +08:00
parent b7ca0b4aca
commit 4e38ee8021
5 changed files with 125 additions and 2 deletions

View File

@@ -0,0 +1,26 @@
// 功能描述:转换配送点写入时的数据库错误,避免向客户端泄露数据库实现细节。
// 版本v1.0
package common
import (
"errors"
"github.com/jackc/pgx/v5/pgconn"
)
const deliveryCodeUniqueConstraint = "idx_delivery_basic_delivery_code"
var errDeliveryCodeExists = errors.New("配送点编码已存在,请更换后重试")
// DeliveryCreateError 将配送点创建错误转换为稳定的业务错误。
// 参数err 为数据库创建操作返回的原始错误。
// 返回值:配送点编码冲突时返回中文业务错误,其他错误保持原样。
func DeliveryCreateError(err error) error {
var postgresError *pgconn.PgError
if errors.As(err, &postgresError) &&
postgresError.Code == "23505" &&
postgresError.ConstraintName == deliveryCodeUniqueConstraint {
return errDeliveryCodeExists
}
return err
}