错误提示有问题
This commit is contained in:
@@ -1,35 +1,37 @@
|
|||||||
package alipay
|
package alipay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/smartwalle/alipay/v3"
|
"github.com/smartwalle/alipay/v3"
|
||||||
|
|
||||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||||
|
apperrors "github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewClientFromConfig 从 WechatConfig 构建支付宝 SDK client。
|
// NewClientFromConfig 从 WechatConfig 构建支付宝 SDK client。
|
||||||
// 校验 ali_app_id、ali_private_key、ali_public_key 必须非空。
|
// 校验 ali_app_id、ali_private_key、ali_public_key 必须非空。
|
||||||
// ali_production=true 时使用生产环境,否则使用沙箱环境。
|
// ali_production=true 时使用生产环境,否则使用沙箱环境。
|
||||||
func NewClientFromConfig(cfg *model.WechatConfig) (*alipay.Client, error) {
|
func NewClientFromConfig(cfg *model.WechatConfig) (*alipay.Client, error) {
|
||||||
|
if cfg == nil {
|
||||||
|
return nil, apperrors.New(apperrors.CodeNoPaymentConfig, "支付宝配置缺失:未找到启用的支付配置")
|
||||||
|
}
|
||||||
if cfg.AliAppID == "" {
|
if cfg.AliAppID == "" {
|
||||||
return nil, fmt.Errorf("支付宝配置缺失:ali_app_id 不能为空")
|
return nil, apperrors.New(apperrors.CodeNoPaymentConfig, "支付宝配置缺失:ali_app_id 不能为空")
|
||||||
}
|
}
|
||||||
if cfg.AliPrivateKey == "" {
|
if cfg.AliPrivateKey == "" {
|
||||||
return nil, fmt.Errorf("支付宝配置缺失:ali_private_key 不能为空")
|
return nil, apperrors.New(apperrors.CodeNoPaymentConfig, "支付宝配置缺失:ali_private_key 不能为空")
|
||||||
}
|
}
|
||||||
if cfg.AliPublicKey == "" {
|
if cfg.AliPublicKey == "" {
|
||||||
return nil, fmt.Errorf("支付宝配置缺失:ali_public_key 不能为空")
|
return nil, apperrors.New(apperrors.CodeNoPaymentConfig, "支付宝配置缺失:ali_public_key 不能为空")
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := alipay.New(cfg.AliAppID, cfg.AliPrivateKey, cfg.AliProduction)
|
client, err := alipay.New(cfg.AliAppID, cfg.AliPrivateKey, cfg.AliProduction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("创建支付宝客户端失败: %w", err)
|
return nil, apperrors.Wrap(apperrors.CodeNoPaymentConfig, err, "支付宝配置不可用:创建客户端失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载支付宝公钥(公钥模式验签)
|
// 加载支付宝公钥(公钥模式验签)
|
||||||
if err := client.LoadAliPayPublicKey(cfg.AliPublicKey); err != nil {
|
if err := client.LoadAliPayPublicKey(cfg.AliPublicKey); err != nil {
|
||||||
return nil, fmt.Errorf("加载支付宝公钥失败: %w", err)
|
return nil, apperrors.Wrap(apperrors.CodeNoPaymentConfig, err, "支付宝配置不可用:加载公钥失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package alipay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/smartwalle/alipay/v3"
|
"github.com/smartwalle/alipay/v3"
|
||||||
|
|
||||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||||
|
apperrors "github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BuildWapPayURL 生成支付宝手机网站支付 URL。
|
// BuildWapPayURL 生成支付宝手机网站支付 URL。
|
||||||
@@ -40,7 +40,7 @@ func BuildWapPayURL(ctx context.Context, cfg *model.WechatConfig, payment *model
|
|||||||
|
|
||||||
payURL, err := client.TradeWapPay(param)
|
payURL, err := client.TradeWapPay(param)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("生成支付宝 WAP 支付链接失败: %w", err)
|
return "", apperrors.Wrap(apperrors.CodeNoPaymentConfig, err, "支付宝配置不可用:生成支付链接失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
return payURL.String(), nil
|
return payURL.String(), nil
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package errors
|
package errors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
stderrors "errors"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -51,12 +52,14 @@ func handleError(c *fiber.Ctx, err error, logger *zap.Logger) error {
|
|||||||
var message string
|
var message string
|
||||||
var httpStatus int
|
var httpStatus int
|
||||||
|
|
||||||
switch e := err.(type) {
|
var appErr *AppError
|
||||||
case *AppError:
|
var fiberErr *fiber.Error
|
||||||
|
switch {
|
||||||
|
case stderrors.As(err, &appErr):
|
||||||
// 应用自定义错误
|
// 应用自定义错误
|
||||||
code = e.Code
|
code = appErr.Code
|
||||||
message = e.Message
|
message = appErr.Message
|
||||||
httpStatus = GetHTTPStatus(e.Code)
|
httpStatus = GetHTTPStatus(appErr.Code)
|
||||||
|
|
||||||
// 记录错误日志(包含完整上下文)
|
// 记录错误日志(包含完整上下文)
|
||||||
logFields := append(errCtx.ToLogFields(),
|
logFields := append(errCtx.ToLogFields(),
|
||||||
@@ -74,16 +77,16 @@ func handleError(c *fiber.Ctx, err error, logger *zap.Logger) error {
|
|||||||
safeLogWithLevel(logger, "warn", "客户端错误", logFields...)
|
safeLogWithLevel(logger, "warn", "客户端错误", logFields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
case *fiber.Error:
|
case stderrors.As(err, &fiberErr):
|
||||||
// Fiber 框架错误
|
// Fiber 框架错误
|
||||||
httpStatus = e.Code
|
httpStatus = fiberErr.Code
|
||||||
code = mapHTTPStatusToCode(httpStatus)
|
code = mapHTTPStatusToCode(httpStatus)
|
||||||
message = GetMessage(code, "zh")
|
message = GetMessage(code, "zh")
|
||||||
|
|
||||||
safeLog(logger, "Fiber 框架错误",
|
safeLog(logger, "Fiber 框架错误",
|
||||||
append(errCtx.ToLogFields(),
|
append(errCtx.ToLogFields(),
|
||||||
zap.Int("http_status", httpStatus),
|
zap.Int("http_status", httpStatus),
|
||||||
zap.String("fiber_message", e.Message),
|
zap.String("fiber_message", fiberErr.Message),
|
||||||
)...,
|
)...,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user