From 894b7bb070175fe26514275ce35b8f1606b62f6a Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 24 Mar 2026 17:02:06 +0100 Subject: [PATCH] Improve error handling and resource cleanup in action handler Refactored InvokeRecActionViewCommandHandler to enhance error handling by logging exceptions and conditionally rethrowing based on ErrorAction. Simplified NTLM HttpClient/handler management for proper disposal. Improved code structure and formatting for clarity. --- .../Commands/InvokeRecActionViewCommand.cs | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs index d4b6aa8..ace9b70 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionViewCommand.cs @@ -1,4 +1,4 @@ -using MediatR; +using MediatR; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Options; using ReC.Application.Common.Constants; @@ -31,28 +31,26 @@ public class InvokeRecActionViewCommandHandler( public async Task Handle(InvokeRecActionViewCommand request, CancellationToken cancel) { var action = request.Action; + HttpClient? ntlmClient = null; - if (action.RestType is not RestType restType) - throw new DataIntegrityException( - $"Rec action could not be invoked because the RestType value is null. " + - $"ProfileId: {action.ProfileId}, " + - $"Id: {action.Id}" - ); + try + { + if (action.RestType is not RestType restType) + throw new DataIntegrityException( + $"Rec action could not be invoked because the RestType value is null. " + + $"ProfileId: {action.ProfileId}, " + + $"Id: {action.Id}" + ); - using var httpReq = CreateHttpRequestMessage(restType, action.EndpointUri); + using var httpReq = CreateHttpRequestMessage(restType, action.EndpointUri); - if (action.Body is not null) - httpReq.Content = new StringContent(action.Body); + if (action.Body is not null) + httpReq.Content = new StringContent(action.Body); - if (action.Headers is not null) - foreach (var header in action.Headers) - httpReq.Headers.Add(header.Key, header.Value); + if (action.Headers is not null) + foreach (var header in action.Headers) + httpReq.Headers.Add(header.Key, header.Value); - HttpClient? ntlmClient = null; - HttpClientHandler? ntlmHandler = null; - - try - { switch (action.EndpointAuthType) { case EndpointAuthType.NoAuth: @@ -116,12 +114,12 @@ public class InvokeRecActionViewCommandHandler( endpointAuthPassword, action.EndpointAuthDomain); var credentialCache = new CredentialCache { { httpReq.RequestUri!, "NTLM", credentials } }; - ntlmHandler = new HttpClientHandler + var ntlmHandler = new HttpClientHandler { Credentials = credentialCache, UseDefaultCredentials = false }; - ntlmClient = new HttpClient(ntlmHandler); + ntlmClient = new HttpClient(ntlmHandler, disposeHandler: true); } break; @@ -139,7 +137,7 @@ public class InvokeRecActionViewCommandHandler( ); } - using var http = ntlmClient ?? clientFactory.CreateClient(Http.ClientName); + var http = ntlmClient ?? clientFactory.CreateClient(Http.ClientName); using var response = await http.SendAsync(httpReq, cancel); var resBody = await response.Content.ReadAsStringAsync(cancel); var resHeaders = response.Headers.ToDictionary(); @@ -154,9 +152,20 @@ public class InvokeRecActionViewCommandHandler( Body = resBody }, cancel); } + catch(Exception ex) + { + await sender.Send(new InsertResultCommand() + { + ActionId = action.Id, + Error = ex.ToString() + }, cancel); + + if (action.ErrorAction == ErrorAction.Stop) + throw; + } finally { - ntlmHandler?.Dispose(); + ntlmClient?.Dispose(); } }