Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active April 20, 2021 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitvijay/e4870f208e622a86f223b43728cc80d0 to your computer and use it in GitHub Desktop.
Save ankitvijay/e4870f208e622a86f223b43728cc80d0 to your computer and use it in GitHub Desktop.
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