Improve HTTP header handling and add logging to handler
Inject optional ILogger into InvokeRecActionViewCommandHandler for enhanced logging. When adding HTTP headers, catch FormatException and log a warning with relevant context, then fall back to TryAddWithoutValidation. This increases robustness and observability for malformed or non-standard headers.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ReC.Application.Common.Constants;
|
||||
using ReC.Application.Common.Dto;
|
||||
@@ -35,7 +36,8 @@ public class InvokeRecActionViewCommandHandler(
|
||||
IOptions<RecActionOptions> options,
|
||||
ISender sender,
|
||||
IHttpClientFactory clientFactory,
|
||||
IConfiguration? config = null
|
||||
IConfiguration? config = null,
|
||||
ILogger<InvokeRecActionViewCommandHandler>? logger = null
|
||||
) : IRequestHandler<InvokeRecActionViewCommand>
|
||||
{
|
||||
private readonly RecActionOptions _options = options.Value;
|
||||
@@ -61,7 +63,12 @@ public class InvokeRecActionViewCommandHandler(
|
||||
|
||||
if (action.Headers is not null)
|
||||
foreach (var header in action.Headers)
|
||||
httpReq.Headers.Add(header.Key, header.Value);
|
||||
try { httpReq.Headers.Add(header.Key, header.Value); }
|
||||
catch (FormatException ex)
|
||||
{
|
||||
logger?.LogWarning(ex, "Header '{Key}' could not be added with strict validation, falling back to TryAddWithoutValidation. ActionId: {ActionId}, ProfileId: {ProfileId}", header.Key, action.Id, action.ProfileId);
|
||||
httpReq.Headers.TryAddWithoutValidation(header.Key, header.Value);
|
||||
}
|
||||
|
||||
switch (action.EndpointAuthType)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user