Skip to content

Commit 16c25b7

Browse files
author
yangyile
committed
让消费函数返回底层的错误原因
1 parent 1b2c63d commit 16c25b7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

rabbitmq/consume.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func ConsumeMessage(ctx context.Context, cfg *Config, handleFunc HandleFunc, max
2020
var onceLimit sync.Once
2121
var waitGroup sync.WaitGroup
2222
var outReason ReasonType
23+
var outErrors error
2324
waitGroup.Add(1) //在线程内部的某处会完成它
2425
go func() {
2526
for {
@@ -29,6 +30,7 @@ func ConsumeMessage(ctx context.Context, cfg *Config, handleFunc HandleFunc, max
2930
//假如已经成功初始化,这里就进不来,而只要能进来就必然是未初始化的
3031
onceLimit.Do(func() {
3132
outReason = must.Sane(reason, MqCannotInit)
33+
outErrors = err
3234
waitGroup.Done()
3335
})
3436
//假如首次连接就失败,就不重试连接,因为首次错误往往是配置问题,需要把错误返回给上层让他们选择
@@ -56,17 +58,19 @@ func ConsumeMessage(ctx context.Context, cfg *Config, handleFunc HandleFunc, max
5658
waitGroup.Wait() //前面是 go routine,但首次初始化还是要确认它能成功,因此这里会等待确认第一次连接成功
5759
log.DebugLog("rabbit-mq-init-success", zap.String("queue_name", cfg.QueueName))
5860
if outReason == MqCannotInit {
59-
return erero.New(string(MqCannotInit))
61+
must.True(outErrors != nil)
62+
return erero.WithMessage(outErrors, string(MqCannotInit))
6063
}
6164
must.Zero(outReason)
65+
must.Done(outErrors)
6266
return nil
6367
}
6468

6569
type ReasonType string
6670

6771
const (
6872
ContextIsDone ReasonType = "context-is-done"
69-
MqCannotInit ReasonType = "mq-cannot-init"
73+
MqCannotInit ReasonType = "mq-can-not-init"
7074
MqDisconnected ReasonType = "mq-disconnected"
7175
)
7276

0 commit comments

Comments
 (0)