From 15581655815bb815a52e86b7c603595eb3f645ca Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 18 Jan 2022 14:58:24 +0200 Subject: [PATCH] CorrelationManager --- .../CommandHandlers/PingMachineHandler.cs | 7 +------ .../DependencyInjectionExtensions.cs | 2 ++ .../Services/CorrelationManager.cs | 14 ++++++++++++++ .../Controllers/ErrorsController.cs | 17 ++++++++++++++--- src/api/NetworkResurrector.Api/Startup.cs | 2 -- 5 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 src/api/NetworkResurrector.Api.Application/Services/CorrelationManager.cs diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs index 2c8b7b4..4607308 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs @@ -1,6 +1,5 @@ using MediatR; using Microsoft.Extensions.Logging; -using NetworkResurrector.Api.Domain.Abstractions; using NetworkResurrector.Api.Domain.Constants; using NetworkResurrector.Api.Domain.Repositories; using NetworkResurrector.Api.PublishedLanguage.Commands; @@ -17,20 +16,16 @@ namespace NetworkResurrector.Api.Application.CommandHandlers private readonly ILogger _logger; private readonly IResurrectorService _resurrectorService; private readonly INetworkRepository _repository; - private readonly IUserService _userService; - public PingMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository, IUserService userService) + public PingMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository) { _logger=logger; _resurrectorService=resurrectorService; _repository=repository; - _userService=userService; } public async Task Handle(PingMachine command, CancellationToken cancellationToken) { - var userId = _userService.GetUserId(); - _logger.LogDebug($"Start pinging machine {command.MachineId}"); var machine = await _repository.GetMachine(command.MachineId); var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.PING); diff --git a/src/api/NetworkResurrector.Api.Application/DependencyInjectionExtensions.cs b/src/api/NetworkResurrector.Api.Application/DependencyInjectionExtensions.cs index 8cb9d84..f308c48 100644 --- a/src/api/NetworkResurrector.Api.Application/DependencyInjectionExtensions.cs +++ b/src/api/NetworkResurrector.Api.Application/DependencyInjectionExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using NetworkResurrector.Api.Application.Services; namespace NetworkResurrector.Api.Application { @@ -6,6 +7,7 @@ namespace NetworkResurrector.Api.Application { public static void AddApplicationServices(this IServiceCollection services) { + services.AddScoped(); } } } diff --git a/src/api/NetworkResurrector.Api.Application/Services/CorrelationManager.cs b/src/api/NetworkResurrector.Api.Application/Services/CorrelationManager.cs new file mode 100644 index 0000000..d4b6ef1 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/Services/CorrelationManager.cs @@ -0,0 +1,14 @@ +using System; + +namespace NetworkResurrector.Api.Application.Services +{ + public class CorrelationManager + { + public Guid CorrelationId { get; } + + public CorrelationManager() + { + CorrelationId=Guid.NewGuid(); + } + } +} diff --git a/src/api/NetworkResurrector.Api/Controllers/ErrorsController.cs b/src/api/NetworkResurrector.Api/Controllers/ErrorsController.cs index b6109f7..0658724 100644 --- a/src/api/NetworkResurrector.Api/Controllers/ErrorsController.cs +++ b/src/api/NetworkResurrector.Api/Controllers/ErrorsController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using NetworkResurrector.Api.Application.Services; using System; using System.ComponentModel.DataAnnotations; @@ -11,17 +12,27 @@ namespace NetworkResurrector.Api.Controllers [ApiExplorerSettings(IgnoreApi = true)] public class ErrorsController : ControllerBase { + private readonly CorrelationManager _correlationManager; + + public ErrorsController(CorrelationManager correlationManager) + { + _correlationManager=correlationManager; + } + + internal record Error(int Status, string Title, Guid CorrelationId, string Message = null); + [Route("error")] public IActionResult HandleErrors() { + var correlationId = _correlationManager.CorrelationId; var context = HttpContext.Features.Get(); var exception = context.Error; return exception switch { - ValidationException => StatusCode(StatusCodes.Status404NotFound, new { exception.Message }), - UnauthorizedAccessException => StatusCode(StatusCodes.Status401Unauthorized, new { exception.Message }), - _ => StatusCode(StatusCodes.Status500InternalServerError), + ValidationException => StatusCode(StatusCodes.Status404NotFound, new Error(StatusCodes.Status404NotFound, "Internal server error", correlationId, exception.Message)), + UnauthorizedAccessException => StatusCode(StatusCodes.Status401Unauthorized, new Error(StatusCodes.Status401Unauthorized, "Internal server error", correlationId)), + _ => StatusCode(StatusCodes.Status500InternalServerError, new Error(StatusCodes.Status500InternalServerError, "Internal server error", correlationId)), }; } } diff --git a/src/api/NetworkResurrector.Api/Startup.cs b/src/api/NetworkResurrector.Api/Startup.cs index f4378bd..bc7edbe 100644 --- a/src/api/NetworkResurrector.Api/Startup.cs +++ b/src/api/NetworkResurrector.Api/Startup.cs @@ -78,8 +78,6 @@ namespace NetworkResurrector.Api app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); app.UseExceptionHandler("/error"); - - app.UseRouting(); app.UseAuthentication(); app.UseAuthorization();