Files
platforms/backend/api/internal/logic/common/delivery_error.go

27 lines
869 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 功能描述:转换配送点写入时的数据库错误,避免向客户端泄露数据库实现细节。
// 版本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
}