Skip to content

Commit 8cd38bc

Browse files
Merge pull request #37 from algojogacor/fix/capture-exception-details
fix: capture exception details for 500/405 responses with empty body
2 parents 640688e + d7702f2 commit 8cd38bc

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22
using System.Text;
33
using DebugProbe.AspNetCore.Models;
44
using DebugProbe.AspNetCore.Options;
@@ -58,14 +58,18 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
5858
var started = Stopwatch.StartNew();
5959

6060
var exception = false;
61+
string? exceptionMessage = null;
62+
string? exceptionStackTrace = null;
6163

6264
try
6365
{
6466
await _next(context);
6567
}
66-
catch
68+
catch (Exception ex)
6769
{
6870
exception = true;
71+
exceptionMessage = ex.Message;
72+
exceptionStackTrace = ex.StackTrace;
6973
throw;
7074
}
7175
finally
@@ -100,25 +104,25 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
100104
$"{context.Request.Path}{context.Request.QueryString}",
101105
RequestBody = Trim(requestBody),
102106

103-
104107
// Response
105108
ResponseBody = Trim(responseBody),
106109

110+
// Exception
111+
ExceptionMessage = exceptionMessage,
112+
ExceptionStackTrace = Trim(exceptionStackTrace, max: 4000),
107113

108114
// Headers
109115
Headers = context.Request.Headers.ToDictionary(x => x.Key, x => x.Value.ToString()),
110116

111-
112117
// Other
113118
Timestamp = DateTime.UtcNow,
114-
115119
});
116120
}
117121
}
118122

119-
private string Trim(string value, int max = 2000)
123+
private string Trim(string? value, int max = 2000)
120124
{
121-
if (string.IsNullOrEmpty(value)) return value;
125+
if (string.IsNullOrEmpty(value)) return value ?? string.Empty;
122126
return value.Length <= max ? value : value.Substring(0, max);
123127
}
124-
}
128+
}

DebugProbe.AspNetCore/Models/DebugEntry.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace DebugProbe.AspNetCore.Models;
1+
namespace DebugProbe.AspNetCore.Models;
22

33
public class DebugEntry
44
{
@@ -19,9 +19,13 @@ public class DebugEntry
1919
public long ResponseSize { get; set; }
2020
public string ResponseBody { get; set; } = default!;
2121

22+
// Exception (captured when middleware catches an unhandled error)
23+
public string? ExceptionMessage { get; set; }
24+
public string? ExceptionStackTrace { get; set; }
25+
2226
// Headers
2327
public Dictionary<string, string> Headers { get; set; } = new();
2428

2529
// Metadata
2630
public DateTimeOffset Timestamp { get; set; }
27-
}
31+
}

0 commit comments

Comments
 (0)