Add Domain project and shifted handlers to application.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace FinancialApi;
|
||||
|
||||
public class DomainExceptionHandler : IExceptionHandler
|
||||
{
|
||||
public async ValueTask<bool> TryHandleAsync(
|
||||
HttpContext httpContext,
|
||||
Exception exception,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Only intercept our specific domain/validation exceptions
|
||||
if (exception is InvalidOperationException or ArgumentException)
|
||||
{
|
||||
var problemDetails = new ProblemDetails
|
||||
{
|
||||
Status = StatusCodes.Status400BadRequest,
|
||||
Title = "Domain Validation Error",
|
||||
Detail = exception.Message,
|
||||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1"
|
||||
};
|
||||
|
||||
httpContext.Response.StatusCode = problemDetails.Status.Value;
|
||||
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using FinancialApi;
|
||||
using FinancialApi.Application;
|
||||
using FinancialApi.Infrastructure;
|
||||
using FinancialApi.ServiceDefaults;
|
||||
using JasperFx.CodeGeneration;
|
||||
@@ -11,11 +12,16 @@ using Wolverine.Sqlite;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddExceptionHandler<DomainExceptionHandler>();
|
||||
builder.Services.AddProblemDetails();
|
||||
|
||||
var connectionString = "Data Source=demo.db";
|
||||
|
||||
builder.Services.AddDbContext<AccountDbContext>(options =>
|
||||
options.UseSqlite(connectionString));
|
||||
|
||||
builder.Services.AddScoped<IAccountQuery, AccountQuery>();
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
@@ -33,6 +39,8 @@ builder.Host.UseResourceSetupOnStartup();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseExceptionHandler();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AccountDbContext>();
|
||||
|
||||
Reference in New Issue
Block a user