Skip to content

Commit cb9c275

Browse files
authored
Merge pull request #40 from PandaTechAM/development
Exception extension compiler enhancment and new features has been added
2 parents 765712a + 7f81289 commit cb9c275

File tree

10 files changed

+314
-54
lines changed

10 files changed

+314
-54
lines changed

Readme.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,31 +128,37 @@ decimal? price = -10.5m;
128128
//For 400 Bad Request
129129
BadRequestException.ThrowIfNullOrNegative(price, "Price is negative");
130130
//For 500 Internal Server Error
131-
InternalServerErrorException.ThrowIfNullOrNegative(price, "Price");
131+
InternalServerErrorException.ThrowIfNullOrNegative(price, "Price is negative");
132132

133133
string? username = " ";
134134
//For 400 Bad Request
135135
BadRequestException.ThrowIfNullOrWhiteSpace(username, "Please provide username");
136136
//For 404 Not Found
137-
NotFoundException.ThrowIfNullOrWhiteSpace(username, "username");
137+
NotFoundException.ThrowIfNullOrWhiteSpace(username);
138138
//For 500 Internal Server Error
139-
InternalServerErrorException.ThrowIfNullOrWhiteSpace(username, "username");
139+
InternalServerErrorException.ThrowIfNullOrWhiteSpace(username, "Price is negative");
140140

141141
List<string?>? tags = new List<string?> { "tag1", " ", null };
142142
//For 400 Bad Request
143-
BadRequestException.ThrowIfNullOrWhiteSpace(tags, "tags");
143+
BadRequestException.ThrowIfNullOrWhiteSpace(tags);
144144
//For 404 Not Found
145-
NotFoundException.ThrowIfNullOrWhiteSpace(tags, "tags");
145+
NotFoundException.ThrowIfNullOrWhiteSpace(tags);
146146
//For 500 Internal Server Error
147-
InternalServerErrorException.ThrowIfNullOrWhiteSpace(tags, "tags");
147+
InternalServerErrorException.ThrowIfNullOrWhiteSpace(tags);
148148

149149
object? user = null;
150150
//For 400 Bad Request
151151
BadRequestException.ThrowIfNull(user, "Please provide user");
152152
//For 404 Not Found
153-
NotFoundException.ThrowIfNull(user, "user");
153+
NotFoundException.ThrowIfNull(user, "Please provide user");
154154
//For 500 Internal Server Error
155-
InternalServerErrorException.ThrowIfNull(user, "user");
155+
InternalServerErrorException.ThrowIfNull(user, "Please provide user");
156+
157+
bool userUnauthorized = false;
158+
//For 401 Unauthorized
159+
UnauthorizedException.ThrowIf(userUnauthorized, "User is unauthorized");
160+
//For 500 Internal Server Error
161+
InternalServerErrorException.ThrowIf(userUnauthorized, "User is unauthorized");
156162
```
157163

158164
These examples show how to use the `ThrowIfNullOrNegative`, `ThrowIfNullOrWhiteSpace`, and `ThrowIfNull` helper methods

src/ResponseCrafter/HttpExceptions/ApiException.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
namespace ResponseCrafter.HttpExceptions;
1+
using System.Diagnostics.CodeAnalysis;
22

3-
public abstract class ApiException(int statusCode, string message,
4-
Dictionary<string, string>? errors = null): Exception(message)
3+
namespace ResponseCrafter.HttpExceptions;
4+
5+
public abstract class ApiException(
6+
int statusCode,
7+
string message,
8+
Dictionary<string, string>? errors = null) : Exception(message)
59
{
610
public int StatusCode { get; private set; } = statusCode;
711
public Dictionary<string, string>? Errors { get; private set; } = errors;
Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace ResponseCrafter.HttpExceptions;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace ResponseCrafter.HttpExceptions;
24

35
public class BadRequestException : ApiException
46
{
@@ -14,35 +16,43 @@ public BadRequestException(Dictionary<string, string> errors)
1416
{
1517
}
1618

17-
public static void ThrowIfNullOrNegative(decimal? value, string message)
19+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
1820
{
19-
if (value is < 0 or null)
21+
if (value is null)
2022
{
21-
throw new BadRequestException(message);
23+
throw new BadRequestException(exceptionMessage);
2224
}
2325
}
2426

25-
public static void ThrowIfNullOrWhiteSpace(string? value, string message)
27+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
2628
{
2729
if (string.IsNullOrWhiteSpace(value))
2830
{
29-
throw new BadRequestException(message);
31+
throw new BadRequestException(exceptionMessage);
3032
}
3133
}
3234

33-
public static void ThrowIfNullOrWhiteSpace(List<string?>? values, string message)
35+
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
3436
{
3537
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
3638
{
37-
throw new BadRequestException(message);
39+
throw new BadRequestException(exceptionMessage);
3840
}
3941
}
4042

41-
public static void ThrowIfNull(object? value, string message)
43+
public static void ThrowIf(bool condition, string exceptionMessage)
4244
{
43-
if (value is null)
45+
if (condition)
46+
{
47+
throw new BadRequestException(exceptionMessage);
48+
}
49+
}
50+
51+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
52+
{
53+
if (value is < 0 or null)
4454
{
45-
throw new BadRequestException(message);
55+
throw new BadRequestException(exceptionMessage);
4656
}
4757
}
4858
}
Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,89 @@
1-
namespace ResponseCrafter.HttpExceptions;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace ResponseCrafter.HttpExceptions;
24

35
public class ConflictException(string message = ConflictException.DefaultMessage) : ApiException(409, message)
46
{
5-
private const string DefaultMessage = "the_request_could_not_be_completed_due_to_a_conflict_with_the_current_state_of_the_target_resource.";
7+
private const string DefaultMessage =
8+
"the_request_could_not_be_completed_due_to_a_conflict_with_the_current_state_of_the_target_resource.";
9+
10+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
11+
{
12+
if (value is null)
13+
{
14+
throw new ConflictException(exceptionMessage);
15+
}
16+
}
17+
18+
public static void ThrowIfNull([NotNull] object? value)
19+
{
20+
if (value is null)
21+
{
22+
throw new ConflictException();
23+
}
24+
}
25+
26+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
27+
{
28+
if (string.IsNullOrWhiteSpace(value))
29+
{
30+
throw new ConflictException(exceptionMessage);
31+
}
32+
}
33+
34+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
35+
{
36+
if (string.IsNullOrWhiteSpace(value))
37+
{
38+
throw new ConflictException();
39+
}
40+
}
41+
42+
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
43+
{
44+
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
45+
{
46+
throw new ConflictException(exceptionMessage);
47+
}
48+
}
49+
50+
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values)
51+
{
52+
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
53+
{
54+
throw new ConflictException();
55+
}
56+
}
57+
58+
public static void ThrowIf(bool condition, string exceptionMessage)
59+
{
60+
if (condition)
61+
{
62+
throw new ConflictException(exceptionMessage);
63+
}
64+
}
65+
66+
public static void ThrowIf(bool condition)
67+
{
68+
if (condition)
69+
{
70+
throw new ConflictException();
71+
}
72+
}
73+
74+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
75+
{
76+
if (value is < 0 or null)
77+
{
78+
throw new ConflictException(exceptionMessage);
79+
}
80+
}
81+
82+
public static void ThrowIfNullOrNegative([NotNull] decimal? value)
83+
{
84+
if (value is < 0 or null)
85+
{
86+
throw new ConflictException();
87+
}
88+
}
689
}
Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
1-
namespace ResponseCrafter.HttpExceptions;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace ResponseCrafter.HttpExceptions;
24

35
public class ForbiddenException(string message = ForbiddenException.DefaultMessage) : ApiException(403, message)
46
{
57
private const string DefaultMessage = "you_are_not_authorized_to_perform_this_action.";
6-
}
8+
9+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
10+
{
11+
if (value is null)
12+
{
13+
throw new ForbiddenException(exceptionMessage);
14+
}
15+
}
16+
17+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
18+
{
19+
if (string.IsNullOrWhiteSpace(value))
20+
{
21+
throw new ForbiddenException(exceptionMessage);
22+
}
23+
}
24+
25+
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
26+
{
27+
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
28+
{
29+
throw new ForbiddenException(exceptionMessage);
30+
}
31+
}
32+
33+
public static void ThrowIf(bool condition, string exceptionMessage)
34+
{
35+
if (condition)
36+
{
37+
throw new ForbiddenException(exceptionMessage);
38+
}
39+
}
40+
41+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
42+
{
43+
if (value is < 0 or null)
44+
{
45+
throw new ForbiddenException(exceptionMessage);
46+
}
47+
}
48+
}
Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace ResponseCrafter.HttpExceptions;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace ResponseCrafter.HttpExceptions;
24

35
public class InternalServerErrorException : ApiException
46
{
@@ -13,36 +15,44 @@ public InternalServerErrorException(Dictionary<string, string> errors)
1315
: base(500, DefaultMessage, errors)
1416
{
1517
}
18+
19+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
20+
{
21+
if (value is null)
22+
{
23+
throw new InternalServerErrorException(exceptionMessage);
24+
}
25+
}
1626

17-
public static void ThrowIfNullOrNegative(decimal? value, string? nameOfValue = null)
27+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
1828
{
19-
if (value is < 0 or null)
29+
if (string.IsNullOrWhiteSpace(value))
2030
{
21-
throw new InternalServerErrorException($"{DefaultMessage}{nameOfValue ?? ""}");
31+
throw new InternalServerErrorException(exceptionMessage);
2232
}
2333
}
2434

25-
public static void ThrowIfNull(object? value, string? nameOfValue = null)
35+
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
2636
{
27-
if (value is null)
37+
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
2838
{
29-
throw new InternalServerErrorException($"{DefaultMessage}{nameOfValue ?? ""}");
39+
throw new InternalServerErrorException(exceptionMessage);
3040
}
3141
}
3242

33-
public static void ThrowIfNullOrWhiteSpace(string? value, string? nameOfValue = null)
43+
public static void ThrowIf(bool condition, string exceptionMessage)
3444
{
35-
if (string.IsNullOrWhiteSpace(value))
45+
if (condition)
3646
{
37-
throw new InternalServerErrorException($"{DefaultMessage}{nameOfValue ?? ""}");
47+
throw new InternalServerErrorException(exceptionMessage);
3848
}
3949
}
4050

41-
public static void ThrowIfNullOrWhiteSpace(List<string?>? values, string? nameOfValue = null)
51+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
4252
{
43-
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
53+
if (value is < 0 or null)
4454
{
45-
throw new InternalServerErrorException($"{DefaultMessage}{nameOfValue ?? ""}");
55+
throw new InternalServerErrorException(exceptionMessage);
4656
}
4757
}
4858
}

0 commit comments

Comments
 (0)