fix: 增强SKU异步任务失败错误明细
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 51s

This commit is contained in:
2026-02-25 09:39:21 +08:00
parent 5fcc1e1484
commit 21d7be5a4f
2 changed files with 54 additions and 2 deletions

View File

@@ -396,7 +396,7 @@ public sealed class ProductController(
{
entity.Status = ProductSkuSaveJobStatus.Failed;
entity.FailedCount = entity.ProgressTotal;
entity.ErrorMessage = Truncate(ex.Message, 2000);
entity.ErrorMessage = Truncate(BuildDetailedErrorMessage(ex), 2000);
entity.FinishedAt = DateTime.UtcNow;
await dbContext.SaveChangesAsync(cancellationToken);
return ApiResponse<ProductSkuSaveJobResponse>.Error(ErrorCodes.InternalServerError, "SKU 保存任务创建失败");
@@ -1370,6 +1370,32 @@ public sealed class ProductController(
return value.Length <= maxLength ? value : value[..maxLength];
}
private static string BuildDetailedErrorMessage(Exception ex)
{
var lines = new List<string>();
var current = ex;
var depth = 0;
const int maxDepth = 8;
while (current is not null && depth < maxDepth)
{
var prefix = depth == 0 ? "Exception" : $"Inner[{depth}]";
var message = string.IsNullOrWhiteSpace(current.Message)
? "(no message)"
: current.Message.Trim();
lines.Add($"{prefix} {current.GetType().Name}: {message}");
current = current.InnerException;
depth++;
}
if (current is not null)
{
lines.Add($"Inner[{depth}] ...");
}
return string.Join(Environment.NewLine, lines);
}
private async Task<ProductRelationState> LoadProductRelationStateAsync(
long productId,
long storeId,

View File

@@ -44,7 +44,7 @@ public sealed class ProductSkuSaveJobRunner(
catch (Exception ex)
{
logger.LogError(ex, "SKU 异步保存任务执行失败JobId={JobId}", jobId);
await MarkFailedAsync(jobMeta.Id, ex.Message);
await MarkFailedAsync(jobMeta.Id, BuildDetailedErrorMessage(ex));
}
}
@@ -159,5 +159,31 @@ public sealed class ProductSkuSaveJobRunner(
return value.Length <= maxLength ? value : value[..maxLength];
}
private static string BuildDetailedErrorMessage(Exception ex)
{
var lines = new List<string>();
var current = ex;
var depth = 0;
const int maxDepth = 8;
while (current is not null && depth < maxDepth)
{
var prefix = depth == 0 ? "Exception" : $"Inner[{depth}]";
var message = string.IsNullOrWhiteSpace(current.Message)
? "(no message)"
: current.Message.Trim();
lines.Add($"{prefix} {current.GetType().Name}: {message}");
current = current.InnerException;
depth++;
}
if (current is not null)
{
lines.Add($"Inner[{depth}] ...");
}
return string.Join(Environment.NewLine, lines);
}
private sealed record JobMeta(long Id, long TenantId);
}