Skip to content

Commit ce26399

Browse files
authored
Merge pull request #41 from PandaTechAM/development
Exception extension overloads added
2 parents cb9c275 + 7a98437 commit ce26399

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/ResponseCrafter/HttpExceptions/ForbiddenException.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,29 @@ public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
1414
}
1515
}
1616

17+
public static void ThrowIfNull([NotNull] object? value)
18+
{
19+
if (value is null)
20+
{
21+
throw new ForbiddenException();
22+
}
23+
}
24+
1725
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
1826
{
1927
if (string.IsNullOrWhiteSpace(value))
2028
{
2129
throw new ForbiddenException(exceptionMessage);
2230
}
2331
}
32+
33+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
34+
{
35+
if (string.IsNullOrWhiteSpace(value))
36+
{
37+
throw new ForbiddenException();
38+
}
39+
}
2440

2541
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
2642
{
@@ -29,6 +45,14 @@ public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, stri
2945
throw new ForbiddenException(exceptionMessage);
3046
}
3147
}
48+
49+
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values)
50+
{
51+
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
52+
{
53+
throw new ForbiddenException();
54+
}
55+
}
3256

3357
public static void ThrowIf(bool condition, string exceptionMessage)
3458
{
@@ -37,6 +61,14 @@ public static void ThrowIf(bool condition, string exceptionMessage)
3761
throw new ForbiddenException(exceptionMessage);
3862
}
3963
}
64+
65+
public static void ThrowIf(bool condition)
66+
{
67+
if (condition)
68+
{
69+
throw new ForbiddenException();
70+
}
71+
}
4072

4173
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
4274
{
@@ -45,4 +77,12 @@ public static void ThrowIfNullOrNegative([NotNull] decimal? value, string except
4577
throw new ForbiddenException(exceptionMessage);
4678
}
4779
}
80+
81+
public static void ThrowIfNullOrNegative([NotNull] decimal? value)
82+
{
83+
if (value is < 0 or null)
84+
{
85+
throw new ForbiddenException();
86+
}
87+
}
4888
}

src/ResponseCrafter/HttpExceptions/UnauthorizedException.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,29 @@ public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
1414
}
1515
}
1616

17+
public static void ThrowIfNull([NotNull] object? value)
18+
{
19+
if (value is null)
20+
{
21+
throw new UnauthorizedException();
22+
}
23+
}
24+
1725
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
1826
{
1927
if (string.IsNullOrWhiteSpace(value))
2028
{
2129
throw new UnauthorizedException(exceptionMessage);
2230
}
2331
}
32+
33+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
34+
{
35+
if (string.IsNullOrWhiteSpace(value))
36+
{
37+
throw new UnauthorizedException();
38+
}
39+
}
2440

2541
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
2642
{
@@ -29,6 +45,14 @@ public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, stri
2945
throw new UnauthorizedException(exceptionMessage);
3046
}
3147
}
48+
49+
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values)
50+
{
51+
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
52+
{
53+
throw new UnauthorizedException();
54+
}
55+
}
3256

3357
public static void ThrowIf(bool condition, string exceptionMessage)
3458
{
@@ -37,6 +61,14 @@ public static void ThrowIf(bool condition, string exceptionMessage)
3761
throw new UnauthorizedException(exceptionMessage);
3862
}
3963
}
64+
65+
public static void ThrowIf(bool condition)
66+
{
67+
if (condition)
68+
{
69+
throw new UnauthorizedException();
70+
}
71+
}
4072

4173
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
4274
{
@@ -45,4 +77,12 @@ public static void ThrowIfNullOrNegative([NotNull] decimal? value, string except
4577
throw new UnauthorizedException(exceptionMessage);
4678
}
4779
}
80+
81+
public static void ThrowIfNullOrNegative([NotNull] decimal? value)
82+
{
83+
if (value is < 0 or null)
84+
{
85+
throw new UnauthorizedException();
86+
}
87+
}
4888
}

src/ResponseCrafter/ResponseCrafter.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<Copyright>MIT</Copyright>
99
<PackageIcon>pandatech.png</PackageIcon>
1010
<PackageReadmeFile>Readme.md</PackageReadmeFile>
11-
<Version>2.2.1</Version>
11+
<Version>2.2.2</Version>
1212
<PackageId>Pandatech.ResponseCrafter</PackageId>
1313
<PackageTags>Pandatech, library, exception handler, exception, middleware, Api response</PackageTags>
1414
<Title>ResponseCrafter</Title>
1515
<Description>Handling exceptions, custom Dtos.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-response-crafter</RepositoryUrl>
17-
<PackageReleaseNotes>Exception extension compiler enhancment and new features has been added</PackageReleaseNotes>
17+
<PackageReleaseNotes>Exception extension overloads added</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)