fix(coroutine): 优化协程扩展中的异常处理机制

- 添加 TaskCanceledException 映射以统一取消状态处理
- 保留原始异常调用栈以避免调试时丢失异常来源
- 优先解包业务异常以避免直接暴露 AggregateException
- 使用 ExceptionDispatchInfo.Capture 确保异常栈信息完整
This commit is contained in:
GeWuYou 2026-04-15 08:25:52 +08:00
parent 96ffd49b31
commit 4c0a99d24c

View File

@ -45,6 +45,7 @@ public static class CqrsCoroutineExtensions
if (task.IsCanceled)
{
// 取消态与成功态区分:协程层统一映射为 TaskCanceledException。
var canceledException = new TaskCanceledException(task);
if (onError != null)
{
@ -52,16 +53,18 @@ public static class CqrsCoroutineExtensions
yield break;
}
// 保留原始抛出栈,避免调试时丢失异常来源。
ExceptionDispatchInfo.Capture(canceledException).Throw();
}
if (!task.IsFaulted)
yield break;
// 优先解包业务异常,避免直接暴露 AggregateException。
var exception = task.Exception!.InnerException ?? task.Exception;
if (onError != null)
onError.Invoke(exception);
else
// 继续保留原始栈信息。
ExceptionDispatchInfo.Capture(exception).Throw();
}
}