CorrelationManager
parent
ef3d9c74e0
commit
1558165581
|
@ -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<PingMachineHandler> _logger;
|
||||
private readonly IResurrectorService _resurrectorService;
|
||||
private readonly INetworkRepository _repository;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public PingMachineHandler(ILogger<PingMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository, IUserService userService)
|
||||
public PingMachineHandler(ILogger<PingMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository)
|
||||
{
|
||||
_logger=logger;
|
||||
_resurrectorService=resurrectorService;
|
||||
_repository=repository;
|
||||
_userService=userService;
|
||||
}
|
||||
|
||||
public async Task<MachinePinged> 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);
|
||||
|
|
|
@ -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<CorrelationManager>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
|
||||
namespace NetworkResurrector.Api.Application.Services
|
||||
{
|
||||
public class CorrelationManager
|
||||
{
|
||||
public Guid CorrelationId { get; }
|
||||
|
||||
public CorrelationManager()
|
||||
{
|
||||
CorrelationId=Guid.NewGuid();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<IExceptionHandlerFeature>();
|
||||
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)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,8 +78,6 @@ namespace NetworkResurrector.Api
|
|||
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
|
||||
app.UseExceptionHandler("/error");
|
||||
|
||||
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
|
Loading…
Reference in New Issue