refactor(TaskExtensions): TaskExtensions verallgemeinern, um benutzerdefinierte Ausnahmegeneratoren zu unterstützen

- Feste NotFoundException durch generischen Ausnahmegenerator in ThrowIfNull-Methoden ersetzt.
- Neue Exceptions-Hilfsklasse für die Erstellung gängiger Ausnahmen (NotFound, BadRequest, Forbidden) hinzugefügt.
- Funktionalität der Then-Erweiterungsmethode unverändert beibehalten.
This commit is contained in:
tekh 2025-08-25 12:41:27 +02:00
parent 86d8fcda07
commit e1f793e571
2 changed files with 36 additions and 10 deletions

View File

@ -9,32 +9,34 @@ public static class TaskExtensions
{ {
/// <summary> /// <summary>
/// Awaits the specified task and ensures that the result is not <c>null</c>. /// Awaits the specified task and ensures that the result is not <c>null</c>.
/// If the result is <c>null</c>, a <see cref="NotFoundException"/> is thrown. /// If the result is <c>null</c>, the exception created by factory-method is thrown.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the result.</typeparam> /// <typeparam name="T">The type of the result.</typeparam>
/// <typeparam name="TException">The type of the exception.</typeparam>
/// <param name="task">The task to await.</param> /// <param name="task">The task to await.</param>
/// <param name="exceptionMessage">Optional custom exception message.</param> /// <param name="factory">Exception provider</param>
/// <returns>The awaited result if not <c>null</c>.</returns> /// <returns>The awaited result if not <c>null</c>.</returns>
/// <exception cref="NotFoundException">Thrown if the result is <c>null</c>.</exception> /// <exception>Thrown if the result is <c>null</c>.</exception>
public static async Task<T> ThrowIfNull<T>(this Task<T?> task, string? exceptionMessage = null) public static async Task<T> ThrowIfNull<T, TException>(this Task<T?> task, Func<TException> factory) where TException : Exception
{ {
var result = await task; var result = await task;
return result ?? throw new NotFoundException(exceptionMessage); return result ?? throw factory();
} }
/// <summary> /// <summary>
/// Awaits the specified task and ensures that the result is not <c>empty</c>. /// Awaits the specified task and ensures that the result is not <c>empty</c>.
/// If the result contains no elements, a <see cref="NotFoundException"/> is thrown. /// If the result contains no elements, the exception created by factory-method is thrown.
/// </summary> /// </summary>
/// <typeparam name="T">The element type of the collection.</typeparam> /// <typeparam name="T">The element type of the collection.</typeparam>
/// <typeparam name="TException">The type of the exception.</typeparam>
/// <param name="task">The task to await.</param> /// <param name="task">The task to await.</param>
/// <param name="exceptionMessage">Optional custom exception message.</param> /// <param name="factory">Exception provider</param>
/// <returns>The awaited collection if it is not <c>null</c> or empty.</returns> /// <returns>The awaited collection if it is not <c>null</c> or empty.</returns>
/// <exception cref="NotFoundException">Thrown if the result is <c>null</c> or empty.</exception> /// <exception cref="NotFoundException">Thrown if the result is <c>null</c> or empty.</exception>
public static async Task<IEnumerable<T>> ThrowIfNull<T>(this Task<IEnumerable<T>> task, string? exceptionMessage = null) public static async Task<IEnumerable<T>> ThrowIfNull<T, TException>(this Task<IEnumerable<T>> task, Func<TException> factory) where TException : Exception
{ {
var result = await task; var result = await task;
return result?.Any() ?? false ? result : throw new NotFoundException(exceptionMessage); return result?.Any() ?? false ? result : throw factory();
} }
/// <summary> /// <summary>
@ -51,3 +53,26 @@ public static class TaskExtensions
return act(res); return act(res);
} }
} }
/// <summary>
///
/// </summary>
public static class Exceptions
{
/// <summary>
///
/// </summary>
public static NotFoundException NotFound() => new();
/// <summary>
///
/// </summary>
/// <returns></returns>
public static BadRequestException BadRequest() => new();
/// <summary>
///
/// </summary>
/// <returns></returns>
public static ForbiddenException Forbidden() => new();
}

View File

@ -6,6 +6,7 @@ using DigitalData.Core.Abstraction.Application.DTO;
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries; using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
using MediatR; using MediatR;
using EnvelopeGenerator.Application.Extensions; using EnvelopeGenerator.Application.Extensions;
using DigitalData.Core.Exceptions;
namespace EnvelopeGenerator.Web.Controllers.Test; namespace EnvelopeGenerator.Web.Controllers.Test;
@ -26,7 +27,7 @@ public class TestEnvelopeReceiverController : ControllerBase
[HttpGet] [HttpGet]
public async Task<IActionResult> Get([FromQuery] ReadEnvelopeReceiverQuery q, CancellationToken cancel) public async Task<IActionResult> 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")] [Obsolete("Use MediatR")]
[HttpGet("verify-access-code/{envelope_receiver_id}")] [HttpGet("verify-access-code/{envelope_receiver_id}")]