diff --git a/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs b/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs index b08e839b..8f598e52 100644 --- a/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs +++ b/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs @@ -9,32 +9,34 @@ public static class TaskExtensions { /// /// Awaits the specified task and ensures that the result is not null. - /// If the result is null, a is thrown. + /// If the result is null, the exception created by factory-method is thrown. /// /// The type of the result. + /// The type of the exception. /// The task to await. - /// Optional custom exception message. + /// Exception provider /// The awaited result if not null. - /// Thrown if the result is null. - public static async Task ThrowIfNull(this Task task, string? exceptionMessage = null) + /// Thrown if the result is null. + public static async Task ThrowIfNull(this Task task, Func factory) where TException : Exception { var result = await task; - return result ?? throw new NotFoundException(exceptionMessage); + return result ?? throw factory(); } /// /// Awaits the specified task and ensures that the result is not empty. - /// If the result contains no elements, a is thrown. + /// If the result contains no elements, the exception created by factory-method is thrown. /// /// The element type of the collection. + /// The type of the exception. /// The task to await. - /// Optional custom exception message. + /// Exception provider /// The awaited collection if it is not null or empty. /// Thrown if the result is null or empty. - public static async Task> ThrowIfNull(this Task> task, string? exceptionMessage = null) + public static async Task> ThrowIfNull(this Task> task, Func factory) where TException : Exception { var result = await task; - return result?.Any() ?? false ? result : throw new NotFoundException(exceptionMessage); + return result?.Any() ?? false ? result : throw factory(); } /// @@ -51,3 +53,26 @@ public static class TaskExtensions return act(res); } } + +/// +/// +/// +public static class Exceptions +{ + /// + /// + /// + public static NotFoundException NotFound() => new(); + + /// + /// + /// + /// + public static BadRequestException BadRequest() => new(); + + /// + /// + /// + /// + public static ForbiddenException Forbidden() => new(); +} \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs index 95cbecd3..c6a93bec 100644 --- a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs +++ b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs @@ -6,6 +6,7 @@ using DigitalData.Core.Abstraction.Application.DTO; using EnvelopeGenerator.Application.EnvelopeReceivers.Queries; using MediatR; using EnvelopeGenerator.Application.Extensions; +using DigitalData.Core.Exceptions; namespace EnvelopeGenerator.Web.Controllers.Test; @@ -26,7 +27,7 @@ public class TestEnvelopeReceiverController : ControllerBase [HttpGet] public async Task Get([FromQuery] ReadEnvelopeReceiverQuery q, CancellationToken cancel) - => Ok(await _mediator.Send(q, cancel).ThrowIfNull()); + => Ok(await _mediator.Send(q, cancel).ThrowIfNull(Exceptions.NotFound)); [Obsolete("Use MediatR")] [HttpGet("verify-access-code/{envelope_receiver_id}")]