mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-05 18:09:04 +00:00
Update exception handling (#146)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using API.Core;
|
||||
using Domain.Exceptions;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Infrastructure.Jwt;
|
||||
@@ -6,34 +8,19 @@ using Infrastructure.Repository.Auth;
|
||||
using Infrastructure.Repository.Sql;
|
||||
using Infrastructure.Repository.UserAccount;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Service.Auth.Auth;
|
||||
using Service.UserManagement.User;
|
||||
|
||||
using API.Core.Contracts.Common;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.ConfigureApiBehaviorOptions(options =>
|
||||
{
|
||||
options.InvalidModelStateResponseFactory = context =>
|
||||
{
|
||||
var errors = context.ModelState.Values
|
||||
.SelectMany(v => v.Errors)
|
||||
.Select(e => e.ErrorMessage)
|
||||
.ToList();
|
||||
// Global Exception Filter
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<GlobalExceptionFilter>();
|
||||
});
|
||||
|
||||
var message = errors.Count == 1
|
||||
? errors[0]
|
||||
: string.Join(" ", errors);
|
||||
|
||||
var response = new
|
||||
{
|
||||
message
|
||||
};
|
||||
|
||||
return new BadRequestObjectResult(response);
|
||||
};
|
||||
});
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddOpenApi();
|
||||
@@ -67,6 +54,8 @@ builder.Services.AddScoped<IRegisterService, RegisterService>();
|
||||
builder.Services.AddScoped<ITokenInfrastructure, JwtInfrastructure>();
|
||||
builder.Services.AddScoped<IPasswordInfrastructure, Argon2Infrastructure>();
|
||||
|
||||
// Register the exception filter
|
||||
builder.Services.AddScoped<GlobalExceptionFilter>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -90,6 +79,3 @@ lifetime.ApplicationStopping.Register(() =>
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
// Make Program class accessible to test projects
|
||||
public partial class Program { }
|
||||
|
||||
Reference in New Issue
Block a user