net6conversion (#642)
* Converting to net6 startup and integration test formats * Update to minimal api and using pagetitle * Adjust functional test fixtures Update ApiEndpoints package version * Finish migration to minimal api startup
This commit is contained in:
45
src/PublicApi/Middleware/ExceptionMiddleware.cs
Normal file
45
src/PublicApi/Middleware/ExceptionMiddleware.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using BlazorShared.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.Middleware;
|
||||
|
||||
public class ExceptionMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public ExceptionMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext httpContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(httpContext);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await HandleExceptionAsync(httpContext, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
|
||||
if (exception is DuplicateException duplicationException)
|
||||
{
|
||||
context.Response.StatusCode = (int)HttpStatusCode.Conflict;
|
||||
await context.Response.WriteAsync(new ErrorDetails()
|
||||
{
|
||||
StatusCode = context.Response.StatusCode,
|
||||
Message = duplicationException.Message
|
||||
}.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user