Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bruno-garcia/30e1480f80dfa1a853f2ffa448f9b9ec to your computer and use it in GitHub Desktop.
Save bruno-garcia/30e1480f80dfa1a853f2ffa448f9b9ec to your computer and use it in GitHub Desktop.
ASP.NET Core middleware which return Exception as text on response body (useful when using swagger UI to test API in development instead of default HTML view)
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
internal class ExceptionInResponseMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionInResponseMiddleware> _logger;
public ExceptionInResponseMiddleware(
RequestDelegate next,
ILogger<ExceptionInResponseMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
if (!context.Response.HasStarted)
{
_logger.LogDebug("Caught exception: '{message}'. Sending as part of the response.", ex.Message);
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "text/plain";
var responseBody = ex.ToString();
context.Response.ContentLength = Encoding.UTF8.GetByteCount(responseBody);
await context.Response.WriteAsync(responseBody);
if (!(context.Features.Get<IExceptionHandlerFeature>() is ExceptionHandlerFeature exceptionFeature))
{
exceptionFeature = new ExceptionHandlerFeature();
context.Features.Set<IExceptionHandlerFeature>(exceptionFeature);
}
exceptionFeature.Error = ex;
}
else
{
_logger.LogWarning("Response has started. Cannot modify it.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment