Skip to content

fix: capture exception details for 500/405 responses with empty body#37

Merged
georgidhristov merged 1 commit into
DebugProbe:mainfrom
algojogacor:fix/capture-exception-details
May 15, 2026
Merged

fix: capture exception details for 500/405 responses with empty body#37
georgidhristov merged 1 commit into
DebugProbe:mainfrom
algojogacor:fix/capture-exception-details

Conversation

@algojogacor
Copy link
Copy Markdown
Contributor

@algojogacor algojogacor commented May 15, 2026

Summary

Fixes #36 — When unhandled exceptions occur, DebugProbe showed the response as having status code 500 but with an empty response body, making debugging difficult.

Root Cause

The middleware's try/catch/finally pattern intercepts the response stream via MemoryStream. When await _next(context) throws:

  1. The exception is caught and re-thrown
  2. The finally block reads the MemoryStream — but at this point ASP.NET Core's exception handling middleware hasn't written anything to it yet
  3. Result: stored response body is empty

Solution

Added two fields to DebugEntry and capture exception data in the catch block:

  • ExceptionMessage — the exception's .Message
  • ExceptionStackTrace — the exception's .StackTrace (trimmed to 4000 chars for readability)

This ensures that even when the response body hasn't been written yet by downstream middleware, the DebugProbe UI can display the actual exception that occurred.

Changes

File Change
DebugProbe.AspNetCore/Models/DebugEntry.cs Add ExceptionMessage and ExceptionStackTrace properties
DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs Capture exception details in catch block; increase stack trace trim limit to 4000

Testing

  • Trigger an unhandled exception → verify ExceptionMessage/StackTrace appear in DebugProbe UI
  • Normal successful requests → verify ExceptionMessage/StackTrace are null (no regression)
  • Build passes

When an unhandled exception occurs, the response body written to the
MemoryStream is empty because ASP.NET Core's exception handler hasn't
processed it yet at the point the middleware's finally block runs.

This adds ExceptionMessage and ExceptionStackTrace fields to DebugEntry
and captures them in the catch block, so the DebugProbe UI can display
the actual exception details even when the response body is empty.

Also increase the trim limit for stack traces to 4000 chars for
meaningful diagnostics.

Closes DebugProbe#36
@georgidhristov
Copy link
Copy Markdown
Collaborator

Tested locally. The backend exception capture works correctly and fixes the empty response debugging issue.

The UI currently still shows an empty response body because ExceptionMessage/ExceptionStackTrace are not rendered yet. I will handle the UI support separately in another issue/PR.

Thanks for the contribution.

@georgidhristov georgidhristov merged commit 8cd38bc into DebugProbe:main May 15, 2026
1 check passed
@georgidhristov georgidhristov added bug Something isn't working middleware HTTP pipeline and middleware related issues .NET Pull requests that update .NET code labels May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working middleware HTTP pipeline and middleware related issues .NET Pull requests that update .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

500 and 405 ASP.NET Core responses lose response body

2 participants