ArchSoft.CustomExceptions
1.0.4
dotnet add package ArchSoft.CustomExceptions --version 1.0.4
NuGet\Install-Package ArchSoft.CustomExceptions -Version 1.0.4
<PackageReference Include="ArchSoft.CustomExceptions" Version="1.0.4" />
<PackageVersion Include="ArchSoft.CustomExceptions" Version="1.0.4" />
<PackageReference Include="ArchSoft.CustomExceptions" />
paket add ArchSoft.CustomExceptions --version 1.0.4
#r "nuget: ArchSoft.CustomExceptions, 1.0.4"
#:package ArchSoft.CustomExceptions@1.0.4
#addin nuget:?package=ArchSoft.CustomExceptions&version=1.0.4
#tool nuget:?package=ArchSoft.CustomExceptions&version=1.0.4
ArchSoft.CustomExceptions
English | Português (Brasil)
A comprehensive collection of domain-specific custom exceptions for .NET applications. This library extends ArchSoft.Http.Exceptions to provide typed exceptions for common scenarios like business rules, resource management, security, and more.
Installation
dotnet add package ArchSoft.CustomExceptions
Features
- Domain-specific exceptions – Business, Resource, Security, Concurrency, and more
- Built on HTTP exceptions – Extends
ArchSoft.Http.Exceptionsfor seamless API integration - Factory pattern support – Use
HttpExceptionFactoryandHttpStatusCodeFactoryfrom the base library - Clean architecture friendly – Separate domain errors from transport-layer concerns
Exception Categories
Api
Exceptions for API communication failures.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
RequestException |
BadRequestException |
400 | Invalid API request formatting or parameters |
ResponseException |
InternalServerErrorException |
500 | Invalid or unexpected API response |
Auth
Authentication and authorization exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
AuthenticationException |
ForbiddenException |
403 | Authentication failure (invalid credentials) |
AuthorizationException |
UnauthorizedException |
401 | Authorization failure (insufficient permissions) |
Broker
Message broker exceptions (Kafka, RabbitMQ, etc.).
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
BrokerException |
BadGatewayException |
502 | General broker communication error |
ConsumerException |
BadGatewayException |
502 | Message consumption failure |
ProducerException |
BadGatewayException |
502 | Message publishing failure |
Business
Business rule validation exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
BusinessException |
UnprocessableEntityException |
422 | Generic business rule violation |
RuleException |
UnprocessableEntityException |
422 | Specific business rule violation |
ValidationException |
UnprocessableEntityException |
422 | Input validation failure |
Concurrency
Concurrency control exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
ConcurrencyException |
ConflictException |
409 | Concurrent modification detected |
VersionMismatchException |
ConflictException |
409 | Optimistic locking version conflict |
Configuration
Configuration-related exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
ConfigurationException |
InternalServerErrorException |
500 | Missing or invalid configuration value |
Integration
External service integration exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
IntegrationException |
BadGatewayException |
502 | External service integration failure |
IO
Input/Output operation exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
InputException |
BadRequestException |
400 | Input reading or parsing error |
OutputException |
InternalServerErrorException |
500 | Output writing error |
Operation
General operation exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
OperationException |
ConflictException |
409 | Generic operation failure |
Process
Data processing exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
ProcessException |
UnprocessableEntityException |
422 | General processing error |
MapException |
UnprocessableEntityException |
422 | Object mapping failure |
ParseException |
UnprocessableEntityException |
422 | Data parsing failure |
Resource
Resource management exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
ResourceNotFoundException |
NotFoundException |
404 | Resource does not exist |
ResourceAlreadyExistsException |
ConflictException |
409 | Duplicate resource creation attempt |
ResourceExhaustedException |
TooManyRequestsException |
429 | Resource quota or capacity exceeded |
Resilience
Resilience pattern exceptions (circuit breaker, rate limiting, etc.).
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
RateLimitExceededException |
TooManyRequestsException |
429 | Rate limit quota exceeded |
ThrottlingException |
TooManyRequestsException |
429 | Request throttling applied |
IdempotencyException |
ConflictException |
409 | Idempotency key conflict |
Security
Security-related exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
EncryptionException |
InternalServerErrorException |
500 | Data encryption failure |
DecryptionException |
InternalServerErrorException |
500 | Data decryption failure |
InvalidSignatureException |
ForbiddenException |
403 | Cryptographic signature validation failure |
Unavailable
Service availability exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
UnavailableException |
ServiceUnavailableException |
503 | Service temporarily unavailable |
Unexpected
Unexpected error exceptions.
| Exception | Base HTTP Exception | HTTP Code | When to Use |
|---|---|---|---|
UnexpectedException |
InternalServerErrorException |
500 | Unexpected/unhandled error |
Usage Examples
Basic Usage
using ArchSoft.CustomExceptions.Exceptions.Resource;
public class UserService
{
public User GetUser(Guid id)
{
var user = _repository.Find(id);
if (user == null)
throw new ResourceNotFoundException($"User with id {id} not found");
return user;
}
}
Business Rule Validation
using ArchSoft.CustomExceptions.Exceptions.Business;
public class OrderService
{
public void PlaceOrder(OrderRequest request)
{
if (request.Items.Count == 0)
throw new ValidationException("Order must contain at least one item");
if (request.TotalAmount <= 0)
throw new RuleException("Order total must be greater than zero");
// Process order...
}
}
Concurrency Control
using ArchSoft.CustomExceptions.Exceptions.Concurrency;
public class DocumentService
{
public void UpdateDocument(Guid id, DocumentUpdate update, long expectedVersion)
{
var document = _repository.Get(id);
if (document.Version != expectedVersion)
throw new VersionMismatchException(
$"Document version mismatch. Expected: {expectedVersion}, Actual: {document.Version}");
// Update document...
}
}
Security Operations
using ArchSoft.CustomExceptions.Exceptions.Security;
public class CryptoService
{
public string Decrypt(string cipherText)
{
try
{
return _encryptionProvider.Decrypt(cipherText);
}
catch (CryptographicException ex)
{
throw new DecryptionException("Failed to decrypt data", ex);
}
}
}
Using Factories
Since all exceptions extend ArchSoft.Http.Exceptions, you can use the factories from that package:
Creating Exceptions by HTTP Status Code
using ArchSoft.Http.Exceptions.Factories;
using System.Net;
// Create appropriate exception based on status code
var ex = HttpExceptionFactory.Create(HttpStatusCode.NotFound, "Resource not found");
throw ex;
Mapping Exceptions to HTTP Status Codes
using ArchSoft.Http.Exceptions.Factories;
using Microsoft.AspNetCore.Http;
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
// Automatically maps custom exceptions to appropriate HTTP status codes
var statusCode = new HttpStatusCodeFactory().Create(ex);
context.Response.StatusCode = (int)statusCode;
await context.Response.WriteAsync(ex.Message);
}
}
Complete Middleware Example
using System.Text.Json;
using ArchSoft.Http.Exceptions.Factories;
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
_logger.LogError(ex, "Unhandled exception occurred");
var statusCode = new HttpStatusCodeFactory().Create(ex);
context.Response.StatusCode = (int)statusCode;
context.Response.ContentType = "application/json";
var response = new
{
error = ex.Message,
statusCode = (int)statusCode,
timestamp = DateTime.UtcNow
};
await context.Response.WriteAsync(JsonSerializer.Serialize(response));
}
}
}
Register in Program.cs:
app.UseMiddleware<ExceptionHandlingMiddleware>();
Requirements
- .NET 8.0 or .NET 9.0 (multi-targeted)
- ArchSoft.Http.Exceptions 1.0.2 or higher
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.
About
Developed by ArchSoft - Software Architecture Solutions
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- ArchSoft.Http.Exceptions (>= 1.0.2)
-
net9.0
- ArchSoft.Http.Exceptions (>= 1.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.