Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Throw Exception from relevant
public class GetOrderQueryHandler
{
.
.
.
public async Task<OrderDto> Handle(GetOrderQuery request, CancellationToken cancellationToken)
{
var order = await _repository.Get(request.OrderId);
if (order == null)
{
throw new ResourceNotFoundException($"Order with {request.OrderId} is not found");
}
.
.
.
}
}
public class Order : IAggregateRoot
{
public int OrderId { get; private set; }
public List<OrderItem> OrderItems { get; private set;} = new List<OrderItem>();
.
.
.
public void ProcessOrder()
{
if (!OrderItems.Any())
{
throw new DomainException("Cannot process order when no items are added");
}
.
.
.
}
}
public class CustomAuthorizationHandler : AuthorizationHandler<CustomRequirement>
{
.
.
.
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context,
CustomRequirement requirement)
{
if (!context.User.HasClaim(e => e.Type == "SuperImportantClaim")
{
throw new UnauthorizedException("User is not authorized");
}
.
.
.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment