refactor: replace Ardalis.Result with ErrorOr across the app
Ardalis Result<T> carried incoherent success sub-statuses and a clunky generic converter; ErrorOr models success-or-errors cleanly. - Application: all commands/queries/handlers now return ErrorOr<T> (non-generic Result -> ErrorOr<Success>); errors via Error.NotFound/ Conflict/Validation/Unauthorized/Failure; success via value or new Success() - Drop ResultExtensions.As<>(): error propagation is now just return .Errors - Rewrite HTTP mapping (ErrorOrHttpExtensions) off Error.Type; success maps to 200 (value) or 204 (Success); Created still built in the endpoints - Endpoints/gateway middleware: !IsSuccess -> IsError - Swap Ardalis.Result package for ErrorOr 2.1.1 (remove unused ref from Worker) Build: 0 warnings, 0 errors.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using Ardalis.Result;
|
||||
using ErrorOr;
|
||||
using Mediator;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -13,21 +13,21 @@ namespace Musify.Application.PlayLists.Handlers
|
||||
IDatabase database,
|
||||
IEventBus eventBus,
|
||||
ILogger<DeletePlayListCommandHandler> logger)
|
||||
: ICommandHandler<DeletePlayListCommand, Result>
|
||||
: ICommandHandler<DeletePlayListCommand, ErrorOr<Success>>
|
||||
{
|
||||
public async ValueTask<Result> Handle(DeletePlayListCommand request, CancellationToken cancellationToken)
|
||||
public async ValueTask<ErrorOr<Success>> Handle(DeletePlayListCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var playList = await database.PlayLists.SingleOrDefaultAsync(p => p.Id == request.PlayListId, cancellationToken);
|
||||
if (playList is null)
|
||||
{
|
||||
logger.LogDebug("Playlist {PlayListId} not found", request.PlayListId);
|
||||
return Result.NotFound();
|
||||
return Error.NotFound();
|
||||
}
|
||||
|
||||
if (playList.UserId != request.UserId)
|
||||
{
|
||||
logger.LogWarning("Playlist {PlayListId} does not belong to user {UserId}", request.PlayListId, request.UserId);
|
||||
return Result.Unauthorized();
|
||||
return Error.Unauthorized();
|
||||
}
|
||||
|
||||
playList.LifeCycleStatus = LifeCycleStatus.Removing;
|
||||
@@ -41,7 +41,7 @@ namespace Musify.Application.PlayLists.Handlers
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish delete playlist event for playlist {PlayListId}", request.PlayListId);
|
||||
return Result.Error($"Failed to delete playlist {request.PlayListId}");
|
||||
return Error.Failure(description: $"Failed to delete playlist {request.PlayListId}");
|
||||
}
|
||||
|
||||
try
|
||||
@@ -51,10 +51,10 @@ namespace Musify.Application.PlayLists.Handlers
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to mark playlist {PlayListId} as removing", request.PlayListId);
|
||||
return Result.Error($"Failed to delete playlist {request.PlayListId}");
|
||||
return Error.Failure(description: $"Failed to delete playlist {request.PlayListId}");
|
||||
}
|
||||
|
||||
return Result.NoContent();
|
||||
return new Success();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user