Add logging to handle null RestType in RecActionCommand

Enhanced the `InvokeRecActionCommandHandler` class by adding
logging functionality using `ILogger`. Updated the constructor
to accept an optional logger parameter. Introduced a check for
`RestType` being `null` and added a warning log to handle such
cases gracefully. This prevents processing invalid actions and
improves system robustness.
This commit is contained in:
Developer 02 2025-11-26 21:54:22 +01:00
parent 73ee0302ef
commit dcc0e8c806

View File

@ -1,4 +1,5 @@
using MediatR; using MediatR;
using Microsoft.Extensions.Logging;
using ReC.Application.RecActions.Queries; using ReC.Application.RecActions.Queries;
namespace ReC.Application.RecActions.Commands; namespace ReC.Application.RecActions.Commands;
@ -13,7 +14,7 @@ public static class InvokeRecActionCommandExtensions
=> sender.Send(new InvokeRecActionCommand { ProfileId = profileId }); => sender.Send(new InvokeRecActionCommand { ProfileId = profileId });
} }
public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory clientFactory) : IRequestHandler<InvokeRecActionCommand> public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory clientFactory, ILogger<InvokeRecActionCommandHandler>? logger = null) : IRequestHandler<InvokeRecActionCommand>
{ {
public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel) public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel)
{ {
@ -28,6 +29,16 @@ public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory cl
await semaphore.WaitAsync(cancel); await semaphore.WaitAsync(cancel);
try try
{ {
if (action.RestType is null)
{
logger?.LogWarning(
"Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}",
action.ProfileId,
action.ActionId
);
return;
}
var method = new HttpMethod(action.RestType.ToUpper()); var method = new HttpMethod(action.RestType.ToUpper());
var msg = new HttpRequestMessage(method, action.EndpointUri); var msg = new HttpRequestMessage(method, action.EndpointUri);