Introduced `JobExceptionHandlingBehavior` to handle exceptions, log errors, and rethrow them during MediatR pipeline execution. Updated `DependencyInjection.cs` to register the new behavior and added a `recClientApiUrl` parameter for API configuration. Enhanced `ProfileMappingProfile.cs` and `GetProfileQuery.cs` with XML documentation for better readability. Improved case-insensitive filtering in `GetProfileQuery` with conditional compilation for .NET version compatibility. Modified `CreateProfileHistoryCommand.cs` to use a non-nullable `AddedWho` property. Added missing `using` directive in `GetProfileQuery.cs` for compatibility. These changes improve code quality, maintainability, and functionality.
48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
using ECMJobRunner.Application.Common.Exceptions;
|
|
using ECMJobRunner.Application.ProfileHistories.Commands;
|
|
using ECMJobRunner.Domain.ValueObjects;
|
|
using MediatR;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ECMJobRunner.Application.Profiles.Commands.Behaviors;
|
|
|
|
/// <summary>
|
|
/// Pipeline behavior that catches <see cref="ECMJobRunner.Application.Common.Exceptions.JobException"/> exceptions,
|
|
/// persists a profile history error record and re-throws the exception
|
|
/// </summary>
|
|
/// <typeparam name="TRequest">The type of the MediatR request</typeparam>
|
|
/// <typeparam name="TResponse">The type of the MediatR response</typeparam>
|
|
/// <param name="Sender">MediatR sender used to dispatch the <see cref="ECMJobRunner.Application.ProfileHistories.Commands.CreateProfileHistoryCommand"/></param>
|
|
public class JobExceptionHandlingBehavior<TRequest, TResponse>(ISender Sender) : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
|
|
{
|
|
/// <summary>
|
|
/// Handles the pipeline behavior
|
|
/// Executes main query if request is TriggeringDEXJobCommand
|
|
/// </summary>
|
|
#if NET48
|
|
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
|
#else
|
|
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
|
|
#endif
|
|
{
|
|
try
|
|
{
|
|
return await next();
|
|
}
|
|
catch (JobException ex)
|
|
{
|
|
var cmd = new CreateProfileHistoryCommand()
|
|
{
|
|
ProfileId = ex.ProfileId,
|
|
Result = ResultType.Ok,
|
|
ResultText = ex.Message,
|
|
AddedWho = "ECMJobRunner"
|
|
};
|
|
await Sender.Send(cmd, cancellationToken);
|
|
|
|
throw;
|
|
}
|
|
}
|
|
}
|