Skip to content

Commit e92c6a8

Browse files
authored
Add FailBadRequest methods to handle bad request scenarios (#30)
* Add FailBadRequest methods to handle bad request scenarios * Update version
1 parent 5466f4d commit e92c6a8

File tree

5 files changed

+106
-33
lines changed

5 files changed

+106
-33
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<RepositoryUrl>https://github.com/managedcode/Communication</RepositoryUrl>
2727
<PackageProjectUrl>https://github.com/managedcode/Communication</PackageProjectUrl>
2828
<Product>Managed Code - Communication</Product>
29-
<Version>9.6.0</Version>
30-
<PackageVersion>9.6.0</PackageVersion>
29+
<Version>9.6.1</Version>
30+
<PackageVersion>9.6.1</PackageVersion>
3131

3232
</PropertyGroup>
3333
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public static CollectionResult<T> Fail(string title)
3333
var problem = Problem.Create(title, title, (int)HttpStatusCode.InternalServerError);
3434
return Create(false, default, 0, 0, 0, problem);
3535
}
36-
36+
3737
public static CollectionResult<T> Fail(string title, string detail)
3838
{
3939
var problem = Problem.Create(title, detail);
4040
return Create(false, default, 0, 0, 0, problem);
4141
}
42-
42+
4343
public static CollectionResult<T> Fail(string title, string detail, HttpStatusCode status)
4444
{
4545
var problem = Problem.Create(title, detail, (int)status);
@@ -50,7 +50,7 @@ public static CollectionResult<T> Fail(Exception exception)
5050
{
5151
return new CollectionResult<T>(false, default, 0, 0, 0, Problem.Create(exception, (int)HttpStatusCode.InternalServerError));
5252
}
53-
53+
5454
public static CollectionResult<T> Fail(Exception exception, HttpStatusCode status)
5555
{
5656
return new CollectionResult<T>(false, default, 0, 0, 0, Problem.Create(exception, (int)status));
@@ -61,6 +61,26 @@ public static CollectionResult<T> FailValidation(params (string field, string me
6161
return new CollectionResult<T>(false, default, 0, 0, 0, Problem.Validation(errors));
6262
}
6363

64+
public static CollectionResult<T> FailBadRequest()
65+
{
66+
var problem = Problem.Create(
67+
ProblemConstants.Titles.BadRequest,
68+
ProblemConstants.Messages.BadRequest,
69+
(int)HttpStatusCode.BadRequest);
70+
71+
return Create(false, default, 0, 0, 0, problem);
72+
}
73+
74+
public static CollectionResult<T> FailBadRequest(string detail)
75+
{
76+
var problem = Problem.Create(
77+
ProblemConstants.Titles.BadRequest,
78+
detail,
79+
(int)HttpStatusCode.BadRequest);
80+
81+
return Create(false, default, 0, 0, 0, problem);
82+
}
83+
6484
public static CollectionResult<T> FailUnauthorized()
6585
{
6686
var problem = Problem.Create(
@@ -70,7 +90,7 @@ public static CollectionResult<T> FailUnauthorized()
7090

7191
return Create(false, default, 0, 0, 0, problem);
7292
}
73-
93+
7494
public static CollectionResult<T> FailUnauthorized(string detail)
7595
{
7696
var problem = Problem.Create(
@@ -90,7 +110,7 @@ public static CollectionResult<T> FailForbidden()
90110

91111
return Create(false, default, 0, 0, 0, problem);
92112
}
93-
113+
94114
public static CollectionResult<T> FailForbidden(string detail)
95115
{
96116
var problem = Problem.Create(
@@ -110,7 +130,7 @@ public static CollectionResult<T> FailNotFound()
110130

111131
return Create(false, default, 0, 0, 0, problem);
112132
}
113-
133+
114134
public static CollectionResult<T> FailNotFound(string detail)
115135
{
116136
var problem = Problem.Create(
@@ -125,7 +145,7 @@ public static CollectionResult<T> Fail<TEnum>(TEnum errorCode) where TEnum : Enu
125145
{
126146
return new CollectionResult<T>(false, default, 0, 0, 0, Problem.Create(errorCode));
127147
}
128-
148+
129149
public static CollectionResult<T> Fail<TEnum>(TEnum errorCode, string detail) where TEnum : Enum
130150
{
131151
return new CollectionResult<T>(false, default, 0, 0, 0, Problem.Create(errorCode, detail));
@@ -135,9 +155,9 @@ public static CollectionResult<T> Fail<TEnum>(TEnum errorCode, HttpStatusCode st
135155
{
136156
return new CollectionResult<T>(false, default, 0, 0, 0, Problem.Create(errorCode, errorCode.ToString(), (int)status));
137157
}
138-
158+
139159
public static CollectionResult<T> Fail<TEnum>(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum
140160
{
141161
return new CollectionResult<T>(false, default, 0, 0, 0, Problem.Create(errorCode, detail, (int)status));
142162
}
143-
}
163+
}

ManagedCode.Communication/Constants/ProblemConstants.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static class Titles
2929
/// </summary>
3030
public static class Messages
3131
{
32+
public const string BadRequest = "The request could not be understood by the server due to malformed syntax.";
3233
public const string UnauthorizedAccess = "Authentication is required to access this resource.";
3334
public const string ForbiddenAccess = "You do not have permission to access this resource.";
3435
public const string ResourceNotFound = "The requested resource was not found.";
@@ -44,7 +45,7 @@ public static class Types
4445
{
4546
public const string AboutBlank = "about:blank";
4647
public const string ValidationFailed = "https://tools.ietf.org/html/rfc7231#section-6.5.1";
47-
48+
4849
public static string HttpStatus(int statusCode) => $"https://httpstatuses.io/{statusCode}";
4950
}
5051

@@ -83,7 +84,7 @@ public static class ExtensionKeys
8384
/// </summary>
8485
public const string OriginalExceptionType = "originalExceptionType";
8586
}
86-
87+
8788
/// <summary>
8889
/// Field names for validation errors.
8990
/// </summary>
@@ -94,4 +95,4 @@ public static class ValidationFields
9495
/// </summary>
9596
public const string General = "_general";
9697
}
97-
}
98+
}

ManagedCode.Communication/Result/Result.Fail.cs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static Result Fail(string title)
3131
var problem = Problem.Create(title, title, HttpStatusCode.InternalServerError);
3232
return Create(false, problem);
3333
}
34-
34+
3535
/// <summary>
3636
/// Creates a failed result with a title and detail.
3737
/// </summary>
@@ -40,7 +40,7 @@ public static Result Fail(string title, string detail)
4040
var problem = Problem.Create(title, detail, HttpStatusCode.InternalServerError);
4141
return Create(false, problem);
4242
}
43-
43+
4444
/// <summary>
4545
/// Creates a failed result with a title, detail and status.
4646
/// </summary>
@@ -57,7 +57,7 @@ public static Result Fail(Exception exception)
5757
{
5858
return Create(false, Problem.Create(exception, (int)HttpStatusCode.InternalServerError));
5959
}
60-
60+
6161
/// <summary>
6262
/// Creates a failed result from an exception with specific status.
6363
/// </summary>
@@ -74,6 +74,32 @@ public static Result FailValidation(params (string field, string message)[] erro
7474
return new Result(false, Problem.Validation(errors));
7575
}
7676

77+
/// <summary>
78+
/// Creates a failed result for bad request.
79+
/// </summary>
80+
public static Result FailBadRequest()
81+
{
82+
var problem = Problem.Create(
83+
ProblemConstants.Titles.BadRequest,
84+
ProblemConstants.Messages.BadRequest,
85+
(int)HttpStatusCode.BadRequest);
86+
87+
return Create(false, problem);
88+
}
89+
90+
/// <summary>
91+
/// Creates a failed result for bad request with custom detail.
92+
/// </summary>
93+
public static Result FailBadRequest(string detail)
94+
{
95+
var problem = Problem.Create(
96+
ProblemConstants.Titles.BadRequest,
97+
detail,
98+
(int)HttpStatusCode.BadRequest);
99+
100+
return Create(false, problem);
101+
}
102+
77103
/// <summary>
78104
/// Creates a failed result for unauthorized access.
79105
/// </summary>
@@ -86,7 +112,7 @@ public static Result FailUnauthorized()
86112

87113
return Create(false, problem);
88114
}
89-
115+
90116
/// <summary>
91117
/// Creates a failed result for unauthorized access with custom detail.
92118
/// </summary>
@@ -112,7 +138,7 @@ public static Result FailForbidden()
112138

113139
return Create(false, problem);
114140
}
115-
141+
116142
/// <summary>
117143
/// Creates a failed result for forbidden access with custom detail.
118144
/// </summary>
@@ -138,7 +164,7 @@ public static Result FailNotFound()
138164

139165
return Create(false, problem);
140166
}
141-
167+
142168
/// <summary>
143169
/// Creates a failed result for not found with custom detail.
144170
/// </summary>
@@ -159,7 +185,7 @@ public static Result Fail<TEnum>(TEnum errorCode) where TEnum : Enum
159185
{
160186
return Create(false, Problem.Create(errorCode));
161187
}
162-
188+
163189
/// <summary>
164190
/// Creates a failed result from a custom error enum with detail.
165191
/// </summary>
@@ -175,12 +201,12 @@ public static Result Fail<TEnum>(TEnum errorCode, HttpStatusCode status) where T
175201
{
176202
return Create(false, Problem.Create(errorCode, errorCode.ToString(), (int)status));
177203
}
178-
204+
179205
/// <summary>
180206
/// Creates a failed result from a custom error enum with detail and specific HTTP status.
181-
/// </summary>і
207+
/// </summary>і
182208
public static Result Fail<TEnum>(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum
183209
{
184210
return Create(false, Problem.Create(errorCode, detail, (int)status));
185211
}
186-
}
212+
}

ManagedCode.Communication/ResultT/ResultT.Fail.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static Result<T> Fail(string title)
4343
var problem = Problem.Create(title, title, (int)HttpStatusCode.InternalServerError);
4444
return Create(false, default, problem);
4545
}
46-
46+
4747
/// <summary>
4848
/// Creates a failed result with a title and detail.
4949
/// </summary>
@@ -52,7 +52,7 @@ public static Result<T> Fail(string title, string detail)
5252
var problem = Problem.Create(title, detail);
5353
return Create(false, default, problem);
5454
}
55-
55+
5656
/// <summary>
5757
/// Creates a failed result with a title, detail and status.
5858
/// </summary>
@@ -69,7 +69,7 @@ public static Result<T> Fail(Exception exception)
6969
{
7070
return new Result<T>(false, default, Problem.Create(exception, (int)HttpStatusCode.InternalServerError));
7171
}
72-
72+
7373
/// <summary>
7474
/// Creates a failed result from an exception with status.
7575
/// </summary>
@@ -86,6 +86,32 @@ public static Result<T> FailValidation(params (string field, string message)[] e
8686
return new Result<T>(false, default, Problem.Validation(errors));
8787
}
8888

89+
/// <summary>
90+
/// Creates a failed result for bad request.
91+
/// </summary>
92+
public static Result<T> FailBadRequest()
93+
{
94+
var problem = Problem.Create(
95+
ProblemConstants.Titles.BadRequest,
96+
ProblemConstants.Messages.BadRequest,
97+
(int)HttpStatusCode.BadRequest);
98+
99+
return Create(false, default, problem);
100+
}
101+
102+
/// <summary>
103+
/// Creates a failed result for bad request with custom detail.
104+
/// </summary>
105+
public static Result<T> FailBadRequest(string detail)
106+
{
107+
var problem = Problem.Create(
108+
ProblemConstants.Titles.BadRequest,
109+
detail,
110+
(int)HttpStatusCode.BadRequest);
111+
112+
return Create(false, default, problem);
113+
}
114+
89115
/// <summary>
90116
/// Creates a failed result for unauthorized access.
91117
/// </summary>
@@ -98,7 +124,7 @@ public static Result<T> FailUnauthorized()
98124

99125
return Create(false, default, problem);
100126
}
101-
127+
102128
/// <summary>
103129
/// Creates a failed result for unauthorized access with custom detail.
104130
/// </summary>
@@ -124,7 +150,7 @@ public static Result<T> FailForbidden()
124150

125151
return Create(false, default, problem);
126152
}
127-
153+
128154
/// <summary>
129155
/// Creates a failed result for forbidden access with custom detail.
130156
/// </summary>
@@ -150,7 +176,7 @@ public static Result<T> FailNotFound()
150176

151177
return Create(false, default, problem);
152178
}
153-
179+
154180
/// <summary>
155181
/// Creates a failed result for not found with custom detail.
156182
/// </summary>
@@ -171,7 +197,7 @@ public static Result<T> Fail<TEnum>(TEnum errorCode) where TEnum : Enum
171197
{
172198
return new Result<T>(false, default, Problem.Create(errorCode));
173199
}
174-
200+
175201
/// <summary>
176202
/// Creates a failed result from a custom error enum with detail.
177203
/// </summary>
@@ -187,12 +213,12 @@ public static Result<T> Fail<TEnum>(TEnum errorCode, HttpStatusCode status) wher
187213
{
188214
return new Result<T>(false, default, Problem.Create(errorCode, errorCode.ToString(), (int)status));
189215
}
190-
216+
191217
/// <summary>
192218
/// Creates a failed result from a custom error enum with detail and specific HTTP status.
193219
/// </summary>
194220
public static Result<T> Fail<TEnum>(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum
195221
{
196222
return new Result<T>(false, default, Problem.Create(errorCode, detail, (int)status));
197223
}
198-
}
224+
}

0 commit comments

Comments
 (0)