middleware
This commit is contained in:
@@ -13,14 +13,7 @@ public class UserController : ControllerBase
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<int>> CreateUser(CreateUserCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
public async Task<ActionResult<int>> CreateUser(CreateUserCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
var userId = await dispatcher.Execute(command, cancellationToken);
|
||||||
{
|
return Ok(userId.IdValue);
|
||||||
var userId = await dispatcher.Execute(command, cancellationToken);
|
|
||||||
return Ok(userId.IdValue);
|
|
||||||
}
|
|
||||||
catch (ValidationException validationException)
|
|
||||||
{
|
|
||||||
return BadRequest(validationException.ValidationErrors);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
47
dotnet/AipsWebApi/Middleware/ExceptionHandlingMiddleware.cs
Normal file
47
dotnet/AipsWebApi/Middleware/ExceptionHandlingMiddleware.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
using AipsCore.Domain.Common.Validation;
|
||||||
|
|
||||||
|
namespace AipsWebApi.Middleware;
|
||||||
|
|
||||||
|
public sealed class ExceptionHandlingMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
||||||
|
|
||||||
|
public ExceptionHandlingMiddleware(
|
||||||
|
RequestDelegate next,
|
||||||
|
ILogger<ExceptionHandlingMiddleware> logger)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Invoke(HttpContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
catch (ValidationException ex)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
|
||||||
|
await context.Response.WriteAsJsonAsync(new
|
||||||
|
{
|
||||||
|
errors = ex.ValidationErrors
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Unhandled exception");
|
||||||
|
|
||||||
|
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
|
||||||
|
await context.Response.WriteAsJsonAsync(new
|
||||||
|
{
|
||||||
|
error = "Something went wrong"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using AipsCore.Infrastructure.DI;
|
using AipsCore.Infrastructure.DI;
|
||||||
|
using AipsWebApi.Middleware;
|
||||||
using DotNetEnv;
|
using DotNetEnv;
|
||||||
|
|
||||||
Env.Load();
|
Env.Load();
|
||||||
@@ -21,6 +22,8 @@ if (app.Environment.IsDevelopment())
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||||
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|||||||
Reference in New Issue
Block a user