fix: 配送点编码重复时返回中文提醒
This commit is contained in:
26
backend/api/internal/logic/common/delivery_error.go
Normal file
26
backend/api/internal/logic/common/delivery_error.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user