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:
parent
86d8fcda07
commit
e1f793e571
@ -9,32 +9,34 @@ public static class TaskExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// <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="exceptionMessage">Optional custom exception message.</param>
|
||||
/// <param name="factory">Exception provider</param>
|
||||
/// <returns>The awaited result if not <c>null</c>.</returns>
|
||||
/// <exception cref="NotFoundException">Thrown if the result is <c>null</c>.</exception>
|
||||
public static async Task<T> ThrowIfNull<T>(this Task<T?> task, string? exceptionMessage = null)
|
||||
/// <exception>Thrown if the result is <c>null</c>.</exception>
|
||||
public static async Task<T> ThrowIfNull<T, TException>(this Task<T?> task, Func<TException> factory) where TException : Exception
|
||||
{
|
||||
var result = await task;
|
||||
return result ?? throw new NotFoundException(exceptionMessage);
|
||||
return result ?? throw factory();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// <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="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>
|
||||
/// <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;
|
||||
return result?.Any() ?? false ? result : throw new NotFoundException(exceptionMessage);
|
||||
return result?.Any() ?? false ? result : throw factory();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -51,3 +53,26 @@ public static class TaskExtensions
|
||||
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();
|
||||
}
|
||||
@ -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<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")]
|
||||
[HttpGet("verify-access-code/{envelope_receiver_id}")]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user