Skip to content

Commit 7e5faf6

Browse files
authored
Merge pull request #42 from PandaTechAM/development
Removed base36 support and added new extension methods on errors
2 parents ce26399 + 2be81d3 commit 7e5faf6

File tree

11 files changed

+296
-248
lines changed

11 files changed

+296
-248
lines changed

Readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ NotFoundException.ThrowIfNullOrWhiteSpace(username);
138138
//For 500 Internal Server Error
139139
InternalServerErrorException.ThrowIfNullOrWhiteSpace(username, "Price is negative");
140140

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

149149
object? user = null;
150150
//For 400 Bad Request
@@ -161,7 +161,7 @@ UnauthorizedException.ThrowIf(userUnauthorized, "User is unauthorized");
161161
InternalServerErrorException.ThrowIf(userUnauthorized, "User is unauthorized");
162162
```
163163

164-
These examples show how to use the `ThrowIfNullOrNegative`, `ThrowIfNullOrWhiteSpace`, and `ThrowIfNull` helper methods
164+
These examples show how to use the `ThrowIfNullOrNegative`, `ThrowIfNullOrWhiteSpace`, `ThrowIfNullOrEmpty` and `ThrowIfNull` helper methods
165165
from `BadRequestException`, `InternalServerErrorException` and `NotFoundException`. Adjust the object names and values according to your specific
166166
application needs.
167167

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Collections;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace ResponseCrafter.HttpExceptions;
45

56
public class BadRequestException : ApiException
67
{
7-
private const string DefaultMessage = "the_request_was_invalid_or_cannot_be_otherwise_served.";
8-
9-
public BadRequestException(string message, Dictionary<string, string>? errors = null)
10-
: base(400, message, errors)
11-
{
12-
}
13-
14-
public BadRequestException(Dictionary<string, string> errors)
15-
: base(400, DefaultMessage, errors)
16-
{
17-
}
18-
19-
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
20-
{
21-
if (value is null)
22-
{
23-
throw new BadRequestException(exceptionMessage);
24-
}
25-
}
26-
27-
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
28-
{
29-
if (string.IsNullOrWhiteSpace(value))
30-
{
31-
throw new BadRequestException(exceptionMessage);
32-
}
33-
}
34-
35-
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
36-
{
37-
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
38-
{
39-
throw new BadRequestException(exceptionMessage);
40-
}
41-
}
42-
43-
public static void ThrowIf(bool condition, string exceptionMessage)
44-
{
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)
54-
{
55-
throw new BadRequestException(exceptionMessage);
56-
}
57-
}
8+
private const string DefaultMessage = "the_request_was_invalid_or_cannot_be_otherwise_served.";
9+
10+
public BadRequestException(string message, Dictionary<string, string>? errors = null)
11+
: base(400, message, errors)
12+
{
13+
}
14+
15+
public BadRequestException(Dictionary<string, string> errors)
16+
: base(400, DefaultMessage, errors)
17+
{
18+
}
19+
20+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
21+
{
22+
if (value is null)
23+
{
24+
throw new BadRequestException(exceptionMessage);
25+
}
26+
}
27+
28+
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string exceptionMessage)
29+
{
30+
// ReSharper disable once GenericEnumeratorNotDisposed
31+
if (value is null || !value.GetEnumerator()
32+
.MoveNext())
33+
{
34+
throw new BadRequestException(exceptionMessage);
35+
}
36+
}
37+
38+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
39+
{
40+
if (string.IsNullOrWhiteSpace(value))
41+
{
42+
throw new BadRequestException(exceptionMessage);
43+
}
44+
}
45+
46+
public static void ThrowIf(bool condition, string exceptionMessage)
47+
{
48+
if (condition)
49+
{
50+
throw new BadRequestException(exceptionMessage);
51+
}
52+
}
53+
54+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
55+
{
56+
if (value is < 0 or null)
57+
{
58+
throw new BadRequestException(exceptionMessage);
59+
}
60+
}
5861
}
Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,95 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Collections;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace ResponseCrafter.HttpExceptions;
45

56
public class ConflictException(string message = ConflictException.DefaultMessage) : ApiException(409, message)
67
{
7-
private const string DefaultMessage =
8-
"the_request_could_not_be_completed_due_to_a_conflict_with_the_current_state_of_the_target_resource.";
8+
private const string DefaultMessage =
9+
"the_request_could_not_be_completed_due_to_a_conflict_with_the_current_state_of_the_target_resource.";
910

10-
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
11-
{
12-
if (value is null)
13-
{
14-
throw new ConflictException(exceptionMessage);
15-
}
16-
}
11+
public static void ThrowIfNull([NotNull] object? value, string exceptionMessage)
12+
{
13+
if (value is null)
14+
{
15+
throw new ConflictException(exceptionMessage);
16+
}
17+
}
1718

18-
public static void ThrowIfNull([NotNull] object? value)
19-
{
20-
if (value is null)
21-
{
22-
throw new ConflictException();
23-
}
24-
}
19+
public static void ThrowIfNull([NotNull] object? value)
20+
{
21+
if (value is null)
22+
{
23+
throw new ConflictException();
24+
}
25+
}
26+
27+
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value)
28+
{
29+
// ReSharper disable once GenericEnumeratorNotDisposed
30+
if (value is null || !value.GetEnumerator()
31+
.MoveNext())
32+
{
33+
throw new ConflictException();
34+
}
35+
}
36+
37+
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string exceptionMessage)
38+
{
39+
// ReSharper disable once GenericEnumeratorNotDisposed
40+
if (value is null || !value.GetEnumerator()
41+
.MoveNext())
42+
{
43+
throw new ConflictException(exceptionMessage);
44+
}
45+
}
2546

26-
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
27-
{
28-
if (string.IsNullOrWhiteSpace(value))
29-
{
30-
throw new ConflictException(exceptionMessage);
31-
}
32-
}
47+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
48+
{
49+
if (string.IsNullOrWhiteSpace(value))
50+
{
51+
throw new ConflictException(exceptionMessage);
52+
}
53+
}
3354

34-
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
35-
{
36-
if (string.IsNullOrWhiteSpace(value))
37-
{
38-
throw new ConflictException();
39-
}
40-
}
55+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
56+
{
57+
if (string.IsNullOrWhiteSpace(value))
58+
{
59+
throw new ConflictException();
60+
}
61+
}
4162

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-
}
4963

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-
}
64+
public static void ThrowIf(bool condition, string exceptionMessage)
65+
{
66+
if (condition)
67+
{
68+
throw new ConflictException(exceptionMessage);
69+
}
70+
}
5771

58-
public static void ThrowIf(bool condition, string exceptionMessage)
59-
{
60-
if (condition)
61-
{
62-
throw new ConflictException(exceptionMessage);
63-
}
64-
}
72+
public static void ThrowIf(bool condition)
73+
{
74+
if (condition)
75+
{
76+
throw new ConflictException();
77+
}
78+
}
6579

66-
public static void ThrowIf(bool condition)
67-
{
68-
if (condition)
69-
{
70-
throw new ConflictException();
71-
}
72-
}
80+
public static void ThrowIfNullOrNegative([NotNull] decimal? value, string exceptionMessage)
81+
{
82+
if (value is < 0 or null)
83+
{
84+
throw new ConflictException(exceptionMessage);
85+
}
86+
}
7387

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-
}
88+
public static void ThrowIfNullOrNegative([NotNull] decimal? value)
89+
{
90+
if (value is < 0 or null)
91+
{
92+
throw new ConflictException();
93+
}
94+
}
8995
}

src/ResponseCrafter/HttpExceptions/ForbiddenException.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Collections;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace ResponseCrafter.HttpExceptions;
45

@@ -21,34 +22,38 @@ public static void ThrowIfNull([NotNull] object? value)
2122
throw new ForbiddenException();
2223
}
2324
}
24-
25-
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
25+
26+
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value)
2627
{
27-
if (string.IsNullOrWhiteSpace(value))
28-
{
29-
throw new ForbiddenException(exceptionMessage);
30-
}
28+
// ReSharper disable once GenericEnumeratorNotDisposed
29+
if (value is null || !value.GetEnumerator()
30+
.MoveNext())
31+
{
32+
throw new ForbiddenException();
33+
}
3134
}
32-
33-
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
35+
36+
public static void ThrowIfNullOrEmpty([NotNull] IEnumerable? value, string exceptionMessage)
3437
{
35-
if (string.IsNullOrWhiteSpace(value))
36-
{
37-
throw new ForbiddenException();
38-
}
38+
// ReSharper disable once GenericEnumeratorNotDisposed
39+
if (value is null || !value.GetEnumerator()
40+
.MoveNext())
41+
{
42+
throw new ForbiddenException(exceptionMessage);
43+
}
3944
}
4045

41-
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values, string exceptionMessage)
46+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value, string exceptionMessage)
4247
{
43-
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
48+
if (string.IsNullOrWhiteSpace(value))
4449
{
4550
throw new ForbiddenException(exceptionMessage);
4651
}
4752
}
4853

49-
public static void ThrowIfNullOrWhiteSpace([NotNull] List<string?>? values)
54+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? value)
5055
{
51-
if (values is null || values.Count == 0 || values.Any(string.IsNullOrWhiteSpace))
56+
if (string.IsNullOrWhiteSpace(value))
5257
{
5358
throw new ForbiddenException();
5459
}

0 commit comments

Comments
 (0)