Skip to content

Commit

Permalink
Merge pull request #35 from PandaTechAM/development
Browse files Browse the repository at this point in the history
Exception throw helpers are added
  • Loading branch information
HaikAsatryan committed Jun 22, 2024
2 parents d6bff85 + 25a57fc commit 3e1bccc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
18 changes: 14 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,36 @@ Using exception helpers:

```csharp
decimal? price = -10.5m;
BadRequestException.ThrowIfNullOrNegative(price, "price");
//For 400 Bad Request
BadRequestException.ThrowIfNullOrNegative(price, "Price is negative");
//For 500 Internal Server Error
InternalServerErrorException.ThrowIfNullOrNegative(price, "Price");

string? username = " ";
//For 400 Bad Request
BadRequestException.ThrowIfNullOrWhiteSpace(username, "username");
BadRequestException.ThrowIfNullOrWhiteSpace(username, "Please provide username");
//For 404 Not Found
NotFoundException.ThrowIfNullOrWhiteSpace(username, "username");
//For 500 Internal Server Error
InternalServerErrorException.ThrowIfNullOrWhiteSpace(username, "username");

List<string?>? tags = new List<string?> { "tag1", " ", null };
//For 400 Bad Request
BadRequestException.ThrowIfNullOrWhiteSpace(tags, "tags");
//For 500 Internal Server Error
InternalServerErrorException.ThrowIfNullOrWhiteSpace(tags, "tags");

object? user = null;
//For 400 Bad Request
BadRequestException.ThrowIfNull(user, "user");
BadRequestException.ThrowIfNull(user, "Please provide user");
//For 404 Not Found
NotFoundException.ThrowIfNull(user, "user");
//For 500 Internal Server Error
InternalServerErrorException.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
from `BadRequestException`, `InternalServerErrorException` and `NotFoundException`. Adjust the object names and values according to your specific
application needs.

## Recommendations
Expand Down
16 changes: 8 additions & 8 deletions src/ResponseCrafter/HttpExceptions/BadRequestException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ public BadRequestException(Dictionary<string, string> errors)
{
}

public static void ThrowIfNullOrNegative(decimal? value, string? nameOfValue = null)
public static void ThrowIfNullOrNegative(decimal? value, string message)
{
if (value is < 0 or null)
{
throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_be_negative");
throw new BadRequestException(message);
}
}

public static void ThrowIfNullOrWhiteSpace(string? value, string? nameOfValue = null)
public static void ThrowIfNullOrWhiteSpace(string? value, string message)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_be_null_or_whitespace");
throw new BadRequestException(message);
}
}

public static void ThrowIfNullOrWhiteSpace(List<string?>? values, string? nameOfValue = null)
public static void ThrowIfNullOrWhiteSpace(List<string?>? values, string message)
{
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
{
throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_contain_null_or_whitespace");
throw new BadRequestException(message);
}
}

public static void ThrowIfNull(object? value, string? nameOfValue = null)
public static void ThrowIfNull(object? value, string message)
{
if (value is null)
{
throw new BadRequestException($"{nameOfValue ?? "value"}_cannot_be_null");
throw new BadRequestException(message);
}
}
}
24 changes: 24 additions & 0 deletions src/ResponseCrafter/HttpExceptions/InternalServerErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,28 @@ public InternalServerErrorException(Dictionary<string, string> errors)
: base(500, DefaultMessage, errors)
{
}

public static void ThrowIfNullOrNegative(decimal? value, string? nameOfValue = null)
{
if (value is < 0 or null)
{
throw new InternalServerErrorException($"{DefaultMessage}{nameOfValue ?? ""}");
}
}

public static void ThrowIfNullOrWhiteSpace(string? value, string? nameOfValue = null)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new InternalServerErrorException($"{DefaultMessage}{nameOfValue ?? ""}");
}
}

public static void ThrowIfNullOrWhiteSpace(List<string?>? values, string? nameOfValue = null)
{
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
{
throw new InternalServerErrorException($"{DefaultMessage}{nameOfValue ?? ""}");
}
}
}
2 changes: 1 addition & 1 deletion src/ResponseCrafter/ResponseCrafter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Copyright>MIT</Copyright>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<PackageId>Pandatech.ResponseCrafter</PackageId>
<PackageTags>Pandatech, library, exception handler, exception, middleware, Api response</PackageTags>
<Title>ResponseCrafter</Title>
Expand Down

0 comments on commit 3e1bccc

Please sign in to comment.