| | | 1 | | using Microsoft.AspNetCore.Http; |
| | | 2 | | using SVETA.Api.Data.DTO; |
| | | 3 | | using SVETA.Api.Helpers.Authorize; |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Microsoft.Extensions.Hosting; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | using Serilog; |
| | | 12 | | using WinSolutions.Sveta.Common; |
| | | 13 | | using ILogger = Serilog.ILogger; |
| | | 14 | | |
| | | 15 | | namespace SVETA.Api.CustomMiddleware |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Перехватывает все исключения при вызове контроллеров и возвращает ошибку в едином формате ErrorDTO |
| | | 19 | | /// </summary> |
| | | 20 | | public class ExceptionMiddleware |
| | | 21 | | { |
| | | 22 | | private readonly RequestDelegate _next; |
| | | 23 | | private readonly IHostEnvironment env; |
| | | 24 | | private readonly ILogger<ExceptionMiddleware> _logger; |
| | | 25 | | |
| | 0 | 26 | | public ExceptionMiddleware(RequestDelegate next, |
| | 0 | 27 | | ILogger<ExceptionMiddleware> logger, |
| | 0 | 28 | | IHostEnvironment environment) |
| | 0 | 29 | | { |
| | 0 | 30 | | env = environment; |
| | 0 | 31 | | _logger = logger; |
| | 0 | 32 | | _next = next; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task InvokeAsync(HttpContext httpContext) |
| | 0 | 36 | | { |
| | | 37 | | try |
| | 0 | 38 | | { |
| | 0 | 39 | | await _next(httpContext); |
| | 0 | 40 | | } |
| | 0 | 41 | | catch (Exception ex) |
| | 0 | 42 | | { |
| | 0 | 43 | | ex.Data["errorcode"] = $"{DateTime.UtcNow.Ticks}"; |
| | 0 | 44 | | _logger.LogError($"Код ошибки: {ex.Data["errorcode"]} {ex.Message} {ex.StackTrace}"); |
| | 0 | 45 | | await HandleExceptionAsync(httpContext, ex, env); |
| | 0 | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | private static Task HandleExceptionAsync(HttpContext context, Exception exception, IHostEnvironment environment) |
| | 0 | 50 | | { |
| | 0 | 51 | | context.Response.ContentType = "application/json"; |
| | | 52 | | |
| | 0 | 53 | | if (exception is ForbidException) |
| | 0 | 54 | | { |
| | 0 | 55 | | context.Response.StatusCode = (int)HttpStatusCode.Forbidden; |
| | 0 | 56 | | } |
| | 0 | 57 | | else if(exception is KeyNotFoundException) |
| | 0 | 58 | | { |
| | 0 | 59 | | context.Response.StatusCode = (int)HttpStatusCode.NotFound; |
| | 0 | 60 | | } |
| | 0 | 61 | | else if(exception is ArgumentException) |
| | 0 | 62 | | { |
| | 0 | 63 | | context.Response.StatusCode = (int)HttpStatusCode.BadRequest; |
| | 0 | 64 | | } |
| | 0 | 65 | | else if(exception is SvetaException) |
| | 0 | 66 | | { |
| | 0 | 67 | | context.Response.StatusCode = (int)HttpStatusCode.BadRequest; |
| | 0 | 68 | | } |
| | 0 | 69 | | else if (exception is AuthorizationException) |
| | 0 | 70 | | { |
| | 0 | 71 | | context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; |
| | 0 | 72 | | } |
| | | 73 | | else |
| | 0 | 74 | | { |
| | 0 | 75 | | context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | object dto; |
| | 0 | 79 | | if (environment.EnvironmentName.Equals("prod", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 80 | | { |
| | 0 | 81 | | dto = new ErrorSimpleDto(exception.Message +$" (Код ошибки: {exception.Data["errorcode"]})" ) |
| | 0 | 82 | | { |
| | 0 | 83 | | code = exception is SvetaException ? (exception as SvetaException).Code : 0 |
| | 0 | 84 | | }; |
| | 0 | 85 | | } |
| | | 86 | | else |
| | 0 | 87 | | { |
| | 0 | 88 | | dto = new ErrorDTO(exception) |
| | 0 | 89 | | { |
| | 0 | 90 | | code = exception is SvetaException ? (exception as SvetaException).Code : 0 |
| | 0 | 91 | | }; |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | return context.Response.WriteAsync(dto.ToString()); |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |