Provides a set of standardized, semantic exceptions for use in the RA.Utilities ecosystem. These exceptions, such as NotFoundException, ConflictException, and BadRequestException, allow for clear, intent-driven error handling in business logic and can be easily mapped to consistent API responses by middleware.
$ dotnet add package RA.Utilities.Core.Exceptions
RA.Utilities.Core.Exceptions provides a set of standardized, semantic exceptions for use across the RA.Utilities ecosystem. These exceptions, such as NotFoundException and ConflictException, allow for clear, intent-driven error handling in your business logic.
Instead of throwing generic Exception or ArgumentException types, this package provides exceptions that carry semantic meaning about what went wrong. This approach has several key benefits:
NotFoundException is more descriptive than a generic exception with a "not found" message.RA.Utilities.Api) can catch these specific exception types and automatically map them to the correct HTTP status codes and structured error responses (e.g., 404 Not Found, 409 Conflict).try-catch blocks in your controllers for common error scenarios.You can install the package via the .NET CLI:
dotnet add package RA.Utilities.Core.Exceptions
Or through the NuGet Package Manager in Visual Studio.
NotFoundExceptionInherits from Exception. Use this when a specific resource or entity cannot be found.
Usage:
public Product GetProductById(int id)
{
var product = _productRepository.Find(id);
if (product == null)
{
throw new NotFoundException("Product", id);
}
return product;
}
When caught by the RA.Utilities.Api middleware, this will typically be translated into an HTTP 404 Not Found response.
ConflictExceptionInherits from Exception. Use this when an action cannot be completed due to a conflict with the current state of a resource, such as trying to create a duplicate item.
Usage:
public void CreateUser(string email)
{
if (_userRepository.Exists(email))
{
throw new ConflictException("User", email);
}
// ... creation logic
}
This will typically be translated into an HTTP 409 Conflict response.
BadRequestExceptionInherits from Exception. Use this for client-side errors, such as invalid input or validation failures that are discovered in the business layer.
Usage:
public void UpdateOrderStatus(int orderId, string newStatus)
{
if (string.IsNullOrWhiteSpace(newStatus))
{
throw new BadRequestException("Status", "Order status cannot be empty.");
}
// ... update logic
}
This will typically be translated into an HTTP 400 Bad Request response.
Contributions are welcome! If you have a suggestion for a new exception type or find a bug, please open an issue to discuss it. Please follow the contribution guidelines outlined in the other projects in this repository.