using EnvelopeGenerator.Application.Exceptions; namespace EnvelopeGenerator.Application.Extensions; /// /// Extension methods for tasks /// public static class TaskExtensions { /// /// Awaits the specified task and ensures that the result is not null. /// If the result is null, a is thrown. /// /// The type of the result. /// The task to await. /// Optional custom exception message. /// The awaited result if not null. /// Thrown if the result is null. public static async Task ThrowIfNull(this Task task, string? exceptionMessage = null) { var result = await task; return result ?? throw new NotFoundException(exceptionMessage); } /// /// Awaits the specified task and ensures that the result is not empty. /// If the result contains no elements, a is thrown. /// /// The element type of the collection. /// The task to await. /// Optional custom exception message. /// 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) { var result = await task; return result?.Any() ?? false ? result : throw new NotFoundException(exceptionMessage); } /// /// /// /// /// /// /// /// public static async Task Then(this Task task, Func act) { var res = await task; return act(res); } }