Skip to content

Commit 4eaace3

Browse files
KarenPoghosyanKarenPoghosyan
authored andcommitted
Enhance exception handling and update version number
Updated `BadRequestException` and `InternalServerErrorException` classes to include overloads for static methods with an optional `Dictionary<string, string>? errors` parameter. This allows for more detailed error handling in methods like `ThrowIfNull`, `ThrowIfNullOrEmpty`, `ThrowIfNullOrWhiteSpace`, `ThrowIf`, and `ThrowIfNullOrNegative`. Incremented version number in `ResponseCrafter.csproj` from `5.1.10` to `5.1.11` and made a minor formatting adjustment in the `<ItemGroup>` section.
1 parent 08a74ec commit 4eaace3

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/ResponseCrafter/HttpExceptions/BadRequestException.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
2525
}
2626
}
2727

28+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage, Dictionary<string, string>? errors = null)
29+
{
30+
if (value is null)
31+
{
32+
throw new BadRequestException(exceptionMessage, errors);
33+
}
34+
}
35+
2836
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string exceptionMessage)
2937
{
3038
// ReSharper disable once GenericEnumeratorNotDisposed
@@ -35,6 +43,15 @@ public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string excep
3543
}
3644
}
3745

46+
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string exceptionMessage, Dictionary<string, string>? errors = null)
47+
{
48+
if (value is null || !value.GetEnumerator()
49+
.MoveNext())
50+
{
51+
throw new BadRequestException(exceptionMessage, errors);
52+
}
53+
}
54+
3855
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
3956
{
4057
if (string.IsNullOrWhiteSpace(value))
@@ -43,6 +60,14 @@ public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string excep
4360
}
4461
}
4562

63+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage, Dictionary<string, string>? errors = null)
64+
{
65+
if (string.IsNullOrWhiteSpace(value))
66+
{
67+
throw new BadRequestException(exceptionMessage, errors);
68+
}
69+
}
70+
4671
public static void ThrowIf(bool condition, string exceptionMessage)
4772
{
4873
if (condition)
@@ -51,11 +76,27 @@ public static void ThrowIf(bool condition, string exceptionMessage)
5176
}
5277
}
5378

79+
public static void ThrowIf(bool condition, string exceptionMessage, Dictionary<string, string>? errors = null)
80+
{
81+
if (condition)
82+
{
83+
throw new BadRequestException(exceptionMessage, errors);
84+
}
85+
}
86+
5487
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
5588
{
5689
if (value is < 0 or null)
5790
{
5891
throw new BadRequestException(exceptionMessage);
5992
}
6093
}
94+
95+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage, Dictionary<string, string>? errors = null)
96+
{
97+
if (value is < 0 or null)
98+
{
99+
throw new BadRequestException(exceptionMessage, errors);
100+
}
101+
}
61102
}

src/ResponseCrafter/HttpExceptions/InternalServerErrorException.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
2525
}
2626
}
2727

28+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage, Dictionary<string, string>? errors = null)
29+
{
30+
if (value is null)
31+
{
32+
throw new InternalServerErrorException(exceptionMessage, errors);
33+
}
34+
}
35+
2836
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string exceptionMessage)
2937
{
3038
// ReSharper disable once GenericEnumeratorNotDisposed
@@ -35,6 +43,15 @@ public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string excep
3543
}
3644
}
3745

46+
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string exceptionMessage, Dictionary<string, string>? errors = null)
47+
{
48+
if (value is null || !value.GetEnumerator()
49+
.MoveNext())
50+
{
51+
throw new InternalServerErrorException(exceptionMessage, errors);
52+
}
53+
}
54+
3855
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
3956
{
4057
if (string.IsNullOrWhiteSpace(value))
@@ -43,6 +60,13 @@ public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string excep
4360
}
4461
}
4562

63+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage, Dictionary<string, string>? errors = null)
64+
{
65+
if (string.IsNullOrWhiteSpace(value))
66+
{
67+
throw new InternalServerErrorException(exceptionMessage, errors);
68+
}
69+
}
4670

4771
public static void ThrowIf(bool condition, string exceptionMessage)
4872
{
@@ -52,11 +76,25 @@ public static void ThrowIf(bool condition, string exceptionMessage)
5276
}
5377
}
5478

79+
public static void ThrowIf(bool condition, string exceptionMessage, Dictionary<string, string>? errors = null)
80+
{
81+
if (condition)
82+
{
83+
throw new InternalServerErrorException(exceptionMessage, errors);
84+
}
85+
}
5586
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
5687
{
5788
if (value is < 0 or null)
5889
{
5990
throw new InternalServerErrorException(exceptionMessage);
6091
}
6192
}
93+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage, Dictionary<string, string>? errors = null)
94+
{
95+
if (value is < 0 or null)
96+
{
97+
throw new BadRequestException(exceptionMessage, errors);
98+
}
99+
}
62100
}

src/ResponseCrafter/ResponseCrafter.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Copyright>MIT</Copyright>
99
<PackageIcon>pandatech.png</PackageIcon>
1010
<PackageReadmeFile>Readme.md</PackageReadmeFile>
11-
<Version>5.1.10</Version>
11+
<Version>5.1.11</Version>
1212
<PackageId>Pandatech.ResponseCrafter</PackageId>
1313
<PackageTags>Pandatech, library, exception handler, exception, middleware, Api response</PackageTags>
1414
<Title>ResponseCrafter</Title>

0 commit comments

Comments
 (0)