-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(CA1031): use exception handler middleware instead of catching all…
… exceptions in ServerRequestHandler.
- Loading branch information
Showing
4 changed files
with
65 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Net.Http; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using MockHttp.Http; | ||
|
||
namespace MockHttp.Server; | ||
|
||
internal static class ExceptionHandler | ||
{ | ||
internal static IApplicationBuilder UseMockHttpExceptionHandler(this IApplicationBuilder app) | ||
{ | ||
return app.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = HandleExceptionAsync }); | ||
} | ||
|
||
private static Task HandleExceptionAsync(HttpContext ctx) | ||
{ | ||
if (ctx.Response.HasStarted) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
Exception? ex = ctx.Features.Get<IExceptionHandlerFeature>()?.Error; | ||
ILogger logger = ctx.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(ExceptionHandler).FullName!); | ||
logger.LogRequestMessage(ctx, Resources.Error_VerifyMockSetup); | ||
|
||
ctx.Response.StatusCode = StatusCodes.Status500InternalServerError; | ||
|
||
IHttpResponseFeature? responseFeature = ctx.Features.Get<IHttpResponseFeature>(); | ||
if (responseFeature is not null) | ||
{ | ||
responseFeature.ReasonPhrase = Resources.Error_VerifyMockSetup; | ||
} | ||
|
||
ctx.Response.ContentType = $"{MediaTypes.PlainText}; charset={MockHttpHandler.DefaultEncoding.WebName}"; | ||
byte[] body = MockHttpHandler.DefaultEncoding.GetBytes(Resources.Error_VerifyMockSetup + Environment.NewLine + ex); | ||
return ctx.Response.BodyWriter.WriteAsync(body, ctx.RequestAborted).AsTask(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters