From e89af1cbcd15131ba8dc213d943ab24cad249207 Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 17 Apr 2026 12:01:15 +0200 Subject: [PATCH] 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. --- .../RecActions/Commands/InvokeRecActionViewCommand.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs index 7a0ea9b..a95c520 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs @@ -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 options, ISender sender, IHttpClientFactory clientFactory, - IConfiguration? config = null + IConfiguration? config = null, + ILogger? logger = null ) : IRequestHandler { 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) {