Release v1.0.0 #16
.envDbInitializer.csDependencyInjection.csPersistence.csprojUberDbContext.cs
.gitea/workflows
ApiDto
ApiDto.csproj
Common
Requests
Responses
Application
Application.csprojDependencyInjection.cs
Backend.slnCommon
Behaviors
Exceptions
Mappings
Cqrs
Campus
Queries
Discipline
Queries
Faculty
Queries
Group
Queries
LectureHall
Queries
Professor
Queries
Schedule
Interfaces
Domain/Schedule
Campus.csDiscipline.csFaculty.csGroup.csLectureHall.csLesson.csLessonAssociation.csProfessor.csSpecificWeek.csTypeOfOccupation.cs
Endpoint
Common
Attributes
BadRequestResponseAttribute.csMaintenanceModeIgnoreAttribute.csNotFoundResponseAttribute.csTokenAuthenticationAttribute.cs
Exceptions
Interfaces
Services
Configuration
EnvironmentManager.cs
General
Swagger
Controllers
BaseController.cs
Endpoint.csprojV1
CampusController.csDisciplineController.csFacultyController.csGroupController.csLectureHallController.csProfessorController.csScheduleController.cs
WeatherForecastController.csMiddleware
Program.csWeatherForecast.csPersistence
Contexts
Schedule
EntityTypeConfigurations
Schedule
60
Endpoint/Middleware/CustomExceptionHandlerMiddleware.cs
Normal file
60
Endpoint/Middleware/CustomExceptionHandlerMiddleware.cs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
using FluentValidation;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Mirea.Api.DataAccess.Application.Common.Exceptions;
|
||||||
|
using Mirea.Api.Dto.Responses;
|
||||||
|
using Mirea.Api.Endpoint.Common.Exceptions;
|
||||||
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Middleware;
|
||||||
|
|
||||||
|
public class CustomExceptionHandlerMiddleware(RequestDelegate next)
|
||||||
|
{
|
||||||
|
public async Task Invoke(HttpContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await next(context);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
await HandleExceptionAsync(context, exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||||
|
{
|
||||||
|
var code = StatusCodes.Status500InternalServerError;
|
||||||
|
var result = string.Empty;
|
||||||
|
switch (exception)
|
||||||
|
{
|
||||||
|
case ValidationException validationException:
|
||||||
|
code = StatusCodes.Status400BadRequest;
|
||||||
|
result = JsonSerializer.Serialize(new ErrorResponse()
|
||||||
|
{
|
||||||
|
Error = validationException.Message,
|
||||||
|
Code = code
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case NotFoundException:
|
||||||
|
code = StatusCodes.Status404NotFound;
|
||||||
|
break;
|
||||||
|
case ControllerArgumentException:
|
||||||
|
code = StatusCodes.Status400BadRequest;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
context.Response.StatusCode = code;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(result))
|
||||||
|
result = JsonSerializer.Serialize(new ErrorResponse()
|
||||||
|
{
|
||||||
|
Error = exception.Message,
|
||||||
|
Code = code
|
||||||
|
});
|
||||||
|
|
||||||
|
return context.Response.WriteAsync(result);
|
||||||
|
}
|
||||||
|
}
|
@ -138,6 +138,7 @@ public class Program
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
app.UseMiddleware<MaintenanceModeMiddleware>();
|
app.UseMiddleware<MaintenanceModeMiddleware>();
|
||||||
|
app.UseMiddleware<CustomExceptionHandlerMiddleware>();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user