Exceptions handling
This commit is contained in:
		| @@ -18,7 +18,7 @@ namespace ApplicationLayer.DataAccessingServices.Locations.RequestHandlers.Admin | |||||||
|  |  | ||||||
|             if (request.Cities.Distinct().Count() < request.Cities.Length) |             if (request.Cities.Distinct().Count() < request.Cities.Length) | ||||||
|             { |             { | ||||||
|                 throw new MultipleIdenticalCitiesInCountry(); |                 throw new MultipleIdenticalCitiesInCountryException(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             var country = new Country |             var country = new Country | ||||||
|   | |||||||
| @@ -2,5 +2,5 @@ | |||||||
| 
 | 
 | ||||||
| namespace ApplicationLayer.DataAccessingServices.Locations.RequestHandlers.AdminRequests.Exceptions | namespace ApplicationLayer.DataAccessingServices.Locations.RequestHandlers.AdminRequests.Exceptions | ||||||
| { | { | ||||||
|     public class MultipleIdenticalCitiesInCountry() : ApiException("There are multiple cities with one name in the country."); |     public class MultipleIdenticalCitiesInCountryException() : ApiException("There are multiple cities with one name in the country."); | ||||||
| } | } | ||||||
| @@ -5,5 +5,5 @@ namespace Infrastructure.Database.GeneralExceptions; | |||||||
| /// Exception to throw when entity not found | /// Exception to throw when entity not found | ||||||
| /// <param name="id">Identifier of entity</param> | /// <param name="id">Identifier of entity</param> | ||||||
| /// <typeparam name="T">Type of entity</typeparam> | /// <typeparam name="T">Type of entity</typeparam> | ||||||
| public class EntityNotFoundByIdException<T>(Guid id) : EntityNotFoundException<T>($"Entity {typeof(T).Name} with id {id} not found.") | public class EntityNotFoundByIdException<T>(Guid id) : EntityNotFoundException<T>($"{typeof(T).Name} with id {id} not found.") | ||||||
|     where T : class, IEntity; |     where T : class, IEntity; | ||||||
| @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Authorization; | |||||||
| using Microsoft.Extensions.Options; | using Microsoft.Extensions.Options; | ||||||
| using Microsoft.IdentityModel.Tokens; | using Microsoft.IdentityModel.Tokens; | ||||||
| using SchengenVisaApi.Common; | using SchengenVisaApi.Common; | ||||||
|  | using SchengenVisaApi.ExceptionFilters; | ||||||
| using Swashbuckle.AspNetCore.SwaggerGen; | using Swashbuckle.AspNetCore.SwaggerGen; | ||||||
|  |  | ||||||
| namespace SchengenVisaApi; | namespace SchengenVisaApi; | ||||||
| @@ -39,7 +40,9 @@ public static class DependencyInjection | |||||||
|             services.AddSwagger(); |             services.AddSwagger(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         services.AddControllers(); |         services.AddProblemDetails(); | ||||||
|  |  | ||||||
|  |         services.AddControllers(opts => opts.Filters.Add<GlobalExceptionsFilter>()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Adds authentication, authorization and token generator |     /// Adds authentication, authorization and token generator | ||||||
|   | |||||||
| @@ -0,0 +1,63 @@ | |||||||
|  | using ApplicationLayer.DataAccessingServices.AuthServices.LoginService.Exceptions; | ||||||
|  | using ApplicationLayer.DataAccessingServices.Locations.RequestHandlers.AdminRequests.Exceptions; | ||||||
|  | using ApplicationLayer.GeneralExceptions; | ||||||
|  | using Domains; | ||||||
|  | using Infrastructure.Database.GeneralExceptions; | ||||||
|  | using Microsoft.AspNetCore.Mvc; | ||||||
|  | using Microsoft.AspNetCore.Mvc.Filters; | ||||||
|  |  | ||||||
|  | namespace SchengenVisaApi.ExceptionFilters | ||||||
|  | { | ||||||
|  |     /// Handles <see cref="ApiException"/> | ||||||
|  |     public class GlobalExceptionsFilter : IAsyncExceptionFilter | ||||||
|  |     { | ||||||
|  |         /// <inheritdoc cref="IExceptionFilter.OnException"/> | ||||||
|  |         public async Task OnExceptionAsync(ExceptionContext context) | ||||||
|  |         { | ||||||
|  |             var exception = context.Exception; | ||||||
|  |             var problemDetails = new ProblemDetails(); | ||||||
|  |  | ||||||
|  |             if (exception is ApiException) | ||||||
|  |             { | ||||||
|  |                 problemDetails.Detail = exception.Message; | ||||||
|  |                 switch (exception) | ||||||
|  |                 { | ||||||
|  |                     case EntityNotFoundException<IEntity>: | ||||||
|  |                         problemDetails.Status = StatusCodes.Status404NotFound; | ||||||
|  |                         problemDetails.Title = "Requested entity not found"; | ||||||
|  |                         problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.4"; | ||||||
|  |                         break; | ||||||
|  |                     case IncorrectLoginDataException: | ||||||
|  |                         problemDetails.Status = StatusCodes.Status403Forbidden; | ||||||
|  |                         problemDetails.Title = "Auth failed"; | ||||||
|  |                         problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.3"; | ||||||
|  |                         break; | ||||||
|  |                     case AlreadyExistsException: | ||||||
|  |                         problemDetails.Status = StatusCodes.Status409Conflict; | ||||||
|  |                         problemDetails.Title = "Already exists"; | ||||||
|  |                         problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.8"; | ||||||
|  |                         break; | ||||||
|  |                     case MultipleIdenticalCitiesInCountryException: | ||||||
|  |                         problemDetails.Status = StatusCodes.Status400BadRequest; | ||||||
|  |                         problemDetails.Title = "Can not add cities with one name to one country"; | ||||||
|  |                         problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1"; | ||||||
|  |                         break; | ||||||
|  |                     default: | ||||||
|  |                         problemDetails.Status = StatusCodes.Status400BadRequest; | ||||||
|  |                         problemDetails.Title = "Bad request"; | ||||||
|  |                         problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1"; | ||||||
|  |                         break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 problemDetails.Status = StatusCodes.Status500InternalServerError; | ||||||
|  |                 problemDetails.Title = "An unhandled error occured"; | ||||||
|  |                 problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.1"; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             await Results.Problem(problemDetails).ExecuteAsync(context.HttpContext); | ||||||
|  |             context.ExceptionHandled = true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -11,6 +11,8 @@ public static class PipelineRequest | |||||||
|  |  | ||||||
|         app.UseHttpsRedirection(); |         app.UseHttpsRedirection(); | ||||||
|  |  | ||||||
|  |         app.UseStatusCodePages(); | ||||||
|  |  | ||||||
|         app.UseAuthentication() |         app.UseAuthentication() | ||||||
|             .UseAuthorization(); |             .UseAuthorization(); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user