diff --git a/Readme.md b/Readme.md index 5016ac0..cfe980a 100644 --- a/Readme.md +++ b/Readme.md @@ -12,9 +12,12 @@ crafting detailed error responses suitable for both development and production e exceptions. * **Detailed Error Responses:** Generates verbose error messages, including stack traces for in-depth debugging in development environments. -* **Environment-Sensitive Logging:** Provides flexible logging and response behavior based on visibility settings (`Public` or `Private`): - - **Private:** All exceptions are sent to the client as defined, and 4xx errors are logged as warnings while 5xx errors are logged as errors. - - **Public:** 4xx exceptions are sent to the client as defined, while 5xx errors are concealed with a generic message. Logging remains the same as in `Private`. +* **Environment-Sensitive Logging:** Provides flexible logging and response behavior based on visibility + settings (`Public` or `Private`): + - **Private:** All exceptions are sent to the client as defined, and 4xx errors are logged as warnings while 5xx + errors are logged as errors. + - **Public:** 4xx exceptions are sent to the client as defined, while 5xx errors are concealed with a generic + message. Logging remains the same as in `Private`. * **Frontend-Friendly Error Messages:** Supports converting error messages to your desired case convention, facilitating easier integration with frontend localization systems. * **Standardized Error Responses:** Provides a standardized error response format, making it easier for frontend @@ -116,6 +119,34 @@ The package automatically logs warnings or errors and provides crafted responses * `InternalServerErrorException` * `ServiceUnavailableException` +### Custom Exception Helper Methods + +Using exception helpers: + +```csharp +decimal? price = -10.5m; +BadRequestException.ThrowIfNullOrNegative(price, "price"); + +string? username = " "; +//For 400 Bad Request +BadRequestException.ThrowIfNullOrWhiteSpace(username, "username"); +//For 404 Not Found +NotFoundException.ThrowIfNullOrWhiteSpace(username, "username"); + +List? tags = new List { "tag1", " ", null }; +BadRequestException.ThrowIfNullOrWhiteSpace(tags, "tags"); + +object? user = null; +//For 400 Bad Request +BadRequestException.ThrowIfNull(user, "user"); +//For 404 Not Found +NotFoundException.ThrowIfNull(user, "user"); +``` + +These examples show how to use the `ThrowIfNullOrNegative`, `ThrowIfNullOrWhiteSpace`, and `ThrowIfNull` helper methods +from `BadRequestException` and `NotFoundException`. Adjust the object names and values according to your specific +application needs. + ## Recommendations * **Error Message Formatting:** It's recommended to use snake_case for error messages to aid frontend applications in diff --git a/src/ResponseCrafter/HttpExceptions/BadRequestException.cs b/src/ResponseCrafter/HttpExceptions/BadRequestException.cs index 7156130..215d17c 100644 --- a/src/ResponseCrafter/HttpExceptions/BadRequestException.cs +++ b/src/ResponseCrafter/HttpExceptions/BadRequestException.cs @@ -3,13 +3,46 @@ public class BadRequestException : ApiException { private const string DefaultMessage = "the_request_was_invalid_or_cannot_be_otherwise_served."; - + public BadRequestException(string message, Dictionary? errors = null) : base(400, message, errors) { } + public BadRequestException(Dictionary errors) - : base(400,DefaultMessage, errors) + : base(400, DefaultMessage, errors) + { + } + + public static void ThrowIfNullOrNegative(decimal? value, string? nameOfValue = null) + { + if (value is < 0 or null) + { + throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_be_negative"); + } + } + + public static void ThrowIfNullOrWhiteSpace(string? value, string? nameOfValue = null) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_be_null_or_whitespace"); + } + } + + public static void ThrowIfNullOrWhiteSpace(List? values, string? nameOfValue = null) + { + if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace)) + { + throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_contain_null_or_whitespace"); + } + } + + public static void ThrowIfNull(object? value, string? nameOfValue = null) { + if (value is null) + { + throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_be_null"); + } } } \ No newline at end of file diff --git a/src/ResponseCrafter/HttpExceptions/NotFoundException.cs b/src/ResponseCrafter/HttpExceptions/NotFoundException.cs index 56ec04c..24bc226 100644 --- a/src/ResponseCrafter/HttpExceptions/NotFoundException.cs +++ b/src/ResponseCrafter/HttpExceptions/NotFoundException.cs @@ -3,4 +3,20 @@ public class NotFoundException(string message = NotFoundException.DefaultMessage) : ApiException(404, message) { private const string DefaultMessage = "the_requested_resource_was_not_found."; + + public static void ThrowIfNullOrWhiteSpace(string? value, string? nameOfValue = null) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new NotFoundException($"{nameOfValue ?? "the_requested_resource"}_was_not_found."); + } + } + + public static void ThrowIfNull(object? value, string? nameOfValue = null) + { + if (value is null) + { + throw new NotFoundException($"{nameOfValue ?? "the_requested_resource"}_was_not_found."); + } + } } diff --git a/src/ResponseCrafter/ResponseCrafter.csproj b/src/ResponseCrafter/ResponseCrafter.csproj index a562caf..df365cd 100644 --- a/src/ResponseCrafter/ResponseCrafter.csproj +++ b/src/ResponseCrafter/ResponseCrafter.csproj @@ -8,13 +8,13 @@ MIT pandatech.png Readme.md - 2.0.0 + 2.1.0 Pandatech.ResponseCrafter Pandatech, library, exception handler, exception, middleware, Api response ResponseCrafter Handling exceptions, custom Dtos. https://github.com/PandaTechAM/be-lib-response-crafter - Dropped support for EfCoreMagicQuery library + Exception throw helpers are added @@ -25,9 +25,9 @@ - + - +