From cc1e2ad7f15801c44907433222638f0a18839c22 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Sun, 10 May 2026 10:04:56 +0300 Subject: [PATCH] fix: store correct status code for unhandled exceptions --- .../Middleware/DebugProbeMiddleware.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs index 9709b86..609f1bf 100644 --- a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs +++ b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs @@ -44,10 +44,17 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) var started = Stopwatch.StartNew(); + var exception = false; + try { await _next(context); } + catch + { + exception = true; + throw; + } finally { started.Stop(); @@ -55,14 +62,16 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) ms.Position = 0; var responseBody = await new StreamReader(ms).ReadToEndAsync(); ms.Position = 0; - await ms.CopyToAsync(originalBody); + await ms.CopyToAsync(originalBody); context.Response.Body = originalBody; var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; var index = shortDatePattern.LastIndexOf('y'); var dataFormat = index >= 0 ? shortDatePattern[..(index + 1)] : shortDatePattern; + var statusCode = exception && context.Response.StatusCode == 200 ? 500 : context.Response.StatusCode; + store.Add(new DebugEntry { Id = Guid.NewGuid().ToString(), @@ -80,7 +89,7 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) Method = context.Request.Method, Path = context.Request.Path, Query = context.Request.QueryString.ToString(), - StatusCode = context.Response.StatusCode, + StatusCode = statusCode, RequestTimeUtc = DateTime.UtcNow, DurationMs = started.ElapsedMilliseconds, RequestSize = Encoding.UTF8.GetByteCount(requestBody),