feat(通知模板): 支持缺字段按空值渲染与空行省略
Motivation: 统一 crypto-strategy 与 trade-signal 两类交易信号的模板字段,使同一套通知模板可复用;字段缺失或为空时不再导致渲染报错或输出空行,通知内容更整洁。 Changes: * 渲染器缺失字段改为按空值处理,新增 line 函数实现空值整行省略 * crypto-strategy 信号补充止盈价、止损价、平均价、收益额等模板字段 * trade-signal 信号保留原始报文中的额外字段以适配统一模板 * 修复结构化日志将 error 作为值直接输出导致的格式问题
This commit is contained in:
@@ -70,6 +70,13 @@ type MessageConverter interface {
|
||||
Convert(body []byte) (event string, data map[string]interface{}, err error)
|
||||
}
|
||||
|
||||
func errText(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
func HandleMessage(ctx context.Context, in HandleInput, conv MessageConverter, lookup SourceLookup, process ProcessFunc) Disposition {
|
||||
owned := false
|
||||
hash := ""
|
||||
@@ -77,7 +84,7 @@ func HandleMessage(ctx context.Context, in HandleInput, conv MessageConverter, l
|
||||
hash = MessageHash(in.Body)
|
||||
ok, err := in.Deduper.Claim(ctx, hash)
|
||||
if err != nil {
|
||||
slog.Warn("dedup claim failed, processing anyway", "hash", hash, "error", err)
|
||||
slog.Warn("dedup claim failed, processing anyway", "hash", hash, "error", errText(err))
|
||||
} else if !ok {
|
||||
slog.Info("duplicate message, ack", "hash", hash)
|
||||
return DispositionAck
|
||||
@@ -94,13 +101,13 @@ func HandleMessage(ctx context.Context, in HandleInput, conv MessageConverter, l
|
||||
|
||||
event, data, err := conv.Convert(in.Body)
|
||||
if err != nil {
|
||||
slog.Warn("invalid signal, ack", "hash", hash, "raw", raw, "error", err)
|
||||
slog.Warn("invalid signal, ack", "hash", hash, "raw", raw, "error", errText(err))
|
||||
return DispositionAck
|
||||
}
|
||||
|
||||
src, err := lookup(ctx, in.SourceName)
|
||||
if err != nil || src == nil || src.Status != 1 {
|
||||
slog.Warn("source unavailable, ack", "source", in.SourceName, "hash", hash, "raw", raw, "error", err)
|
||||
slog.Warn("source unavailable, ack", "source", in.SourceName, "hash", hash, "raw", raw, "error", errText(err))
|
||||
return DispositionAck
|
||||
}
|
||||
|
||||
@@ -116,14 +123,14 @@ func HandleMessage(ctx context.Context, in HandleInput, conv MessageConverter, l
|
||||
return DispositionAck
|
||||
}
|
||||
if errors.Is(err, notify.ErrUnprocessable) {
|
||||
slog.Warn("unprocessable notify, ack", "source", src.Name, "event", event, "hash", hash, "raw", raw, "error", err)
|
||||
slog.Warn("unprocessable notify, ack", "source", src.Name, "event", event, "hash", hash, "raw", raw, "error", errText(err))
|
||||
return DispositionAck
|
||||
}
|
||||
|
||||
disp := DecideRetry(RetryCount(in.Headers), in.MaxRetry)
|
||||
if owned && in.Deduper != nil {
|
||||
if relErr := in.Deduper.Release(ctx, hash); relErr != nil {
|
||||
slog.Warn("dedup release failed", "hash", hash, "error", relErr)
|
||||
slog.Warn("dedup release failed", "hash", hash, "error", errText(relErr))
|
||||
}
|
||||
}
|
||||
return disp
|
||||
|
||||
Reference in New Issue
Block a user