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
|
||||
}
|
||||
44
backend/api/internal/logic/common/delivery_error_test.go
Normal file
44
backend/api/internal/logic/common/delivery_error_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
// 功能描述:验证配送点创建错误的业务化转换规则。
|
||||
// 版本:v1.0
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
// TestDeliveryCreateErrorDeliveryCodeConflict 验证配送点编码唯一冲突会转换为中文提醒。
|
||||
// 参数:t 为 Go 测试上下文。
|
||||
// 返回值:无。
|
||||
func TestDeliveryCreateErrorDeliveryCodeConflict(t *testing.T) {
|
||||
databaseError := &pgconn.PgError{Code: "23505", ConstraintName: deliveryCodeUniqueConstraint}
|
||||
|
||||
actual := DeliveryCreateError(databaseError)
|
||||
if actual.Error() != "配送点编码已存在,请更换后重试" {
|
||||
t.Fatalf("期望中文重复编码提醒,实际为 %q", actual.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeliveryCreateErrorKeepsOtherErrors 验证非目标约束错误不会被误转换。
|
||||
// 参数:t 为 Go 测试上下文。
|
||||
// 返回值:无。
|
||||
func TestDeliveryCreateErrorKeepsOtherErrors(t *testing.T) {
|
||||
original := errors.New("database unavailable")
|
||||
|
||||
if actual := DeliveryCreateError(original); actual != original {
|
||||
t.Fatalf("期望保留原始错误,实际为 %v", actual)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeliveryCreateErrorKeepsOtherUniqueConstraints 验证其他唯一约束冲突不会被误报为配送点编码重复。
|
||||
// 参数:t 为 Go 测试上下文。
|
||||
// 返回值:无。
|
||||
func TestDeliveryCreateErrorKeepsOtherUniqueConstraints(t *testing.T) {
|
||||
databaseError := &pgconn.PgError{Code: "23505", ConstraintName: "idx_other_unique_constraint"}
|
||||
|
||||
if actual := DeliveryCreateError(databaseError); actual != databaseError {
|
||||
t.Fatalf("期望保留其他约束错误,实际为 %v", actual)
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ func CreateDelivery(ctx *gin.Context) {
|
||||
Name: request.Name, Principal: request.Principal, Address: request.Address,
|
||||
}
|
||||
if err := impl.DBService.Create(&delivery).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
infra.Response.Error(ctx, common.DeliveryCreateError(err))
|
||||
return
|
||||
}
|
||||
common.RespondCreatedResource(ctx, delivery)
|
||||
|
||||
@@ -106,7 +106,7 @@ func CreateDeliveryBasic(ctx *gin.Context) {
|
||||
}
|
||||
delivery := models.DeliveryBasic{Entity: common.NewEntity(common.StatusDraft), DeliveryCode: request.DeliveryCode, GasBasicID: gasBasicID, Name: request.Name, Principal: request.Principal, Address: request.Address}
|
||||
if err := impl.DBService.Create(&delivery).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
infra.Response.Error(ctx, common.DeliveryCreateError(err))
|
||||
return
|
||||
}
|
||||
common.RespondCreatedResource(ctx, delivery)
|
||||
|
||||
Reference in New Issue
Block a user