Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Commit c062dba

Browse files
committed
Implementata exception status code 500 - close #12
1 parent 67bb322 commit c062dba

File tree

3 files changed

+127
-23
lines changed

3 files changed

+127
-23
lines changed

src/CustomLibrary.ProblemDetails/Response/Confirm.cs renamed to src/CustomLibrary.ProblemDetails/Confirm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace CustomLibrary.ProblemDetails.Response;
1+
namespace CustomLibrary.ProblemDetails;
22

33
public class Confirm
44
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace CustomLibrary.ProblemDetails.Exception;
2+
3+
public class InternalServerErrorException : System.Exception
4+
{
5+
public InternalServerErrorException()
6+
{
7+
}
8+
9+
public InternalServerErrorException(string message) : base(message)
10+
{
11+
}
12+
13+
public InternalServerErrorException(string message, System.Exception innerException) : base(message, innerException)
14+
{
15+
}
16+
}

src/CustomLibrary.ProblemDetails/Response/Response.cs renamed to src/CustomLibrary.ProblemDetails/ResponseException.cs

Lines changed: 110 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
namespace CustomLibrary.ProblemDetails.Response;
1+
namespace CustomLibrary.ProblemDetails;
22

3-
public static class Response
3+
public static class ResponseException
44
{
5-
public static ObjectResult NotModified(HttpContext httpContext, System.Exception exc)
5+
public static ObjectResult NotModified(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
66
{
77
var statusCode = StatusCodes.Status304NotModified;
88
var problemDetails = new CustomProblemDetails
99
{
1010
Status = statusCode,
11+
Detail = exc.Message,
1112
Type = $"https://httpstatuses.com/{statusCode}",
1213
Instance = httpContext.Request.Path,
1314
Title = "NotModified"
1415
};
1516

1617
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
17-
problemDetails.Extensions.Add("errors", exc.Message);
18+
//problemDetails.Extensions.Add("errors", exc.Message);
19+
20+
if (validationError?.Any() ?? false)
21+
{
22+
problemDetails.Extensions.Add("errors", validationError);
23+
}
1824

1925
var result = new ObjectResult(problemDetails)
2026
{
@@ -24,19 +30,25 @@ public static ObjectResult NotModified(HttpContext httpContext, System.Exception
2430
return result;
2531
}
2632

27-
public static ObjectResult BadRequest(HttpContext httpContext, System.Exception exc)
33+
public static ObjectResult BadRequest(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
2834
{
2935
var statusCode = StatusCodes.Status400BadRequest;
3036
var problemDetails = new CustomProblemDetails
3137
{
3238
Status = statusCode,
39+
Detail = exc.Message,
3340
Type = $"https://httpstatuses.com/{statusCode}",
3441
Instance = httpContext.Request.Path,
3542
Title = "BadRequest"
3643
};
3744

3845
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
39-
problemDetails.Extensions.Add("errors", exc.Message);
46+
//problemDetails.Extensions.Add("errors", exc.Message);
47+
48+
if (validationError?.Any() ?? false)
49+
{
50+
problemDetails.Extensions.Add("errors", validationError);
51+
}
4052

4153
var result = new ObjectResult(problemDetails)
4254
{
@@ -46,19 +58,25 @@ public static ObjectResult BadRequest(HttpContext httpContext, System.Exception
4658
return result;
4759
}
4860

49-
public static ObjectResult Unauthorized(HttpContext httpContext, System.Exception exc)
61+
public static ObjectResult Unauthorized(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
5062
{
5163
var statusCode = StatusCodes.Status401Unauthorized;
5264
var problemDetails = new CustomProblemDetails
5365
{
5466
Status = statusCode,
67+
Detail = exc.Message,
5568
Type = $"https://httpstatuses.com/{statusCode}",
5669
Instance = httpContext.Request.Path,
5770
Title = "Unauthorized"
5871
};
5972

6073
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
61-
problemDetails.Extensions.Add("errors", exc.Message);
74+
//problemDetails.Extensions.Add("errors", exc.Message);
75+
76+
if (validationError?.Any() ?? false)
77+
{
78+
problemDetails.Extensions.Add("errors", validationError);
79+
}
6280

6381
var result = new ObjectResult(problemDetails)
6482
{
@@ -68,19 +86,25 @@ public static ObjectResult Unauthorized(HttpContext httpContext, System.Exceptio
6886
return result;
6987
}
7088

71-
public static ObjectResult Forbidden(HttpContext httpContext, System.Exception exc)
89+
public static ObjectResult Forbidden(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
7290
{
7391
var statusCode = StatusCodes.Status403Forbidden;
7492
var problemDetails = new CustomProblemDetails
7593
{
7694
Status = statusCode,
95+
Detail = exc.Message,
7796
Type = $"https://httpstatuses.com/{statusCode}",
7897
Instance = httpContext.Request.Path,
7998
Title = "Forbidden"
8099
};
81100

82101
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
83-
problemDetails.Extensions.Add("errors", exc.Message);
102+
//problemDetails.Extensions.Add("errors", exc.Message);
103+
104+
if (validationError?.Any() ?? false)
105+
{
106+
problemDetails.Extensions.Add("errors", validationError);
107+
}
84108

85109
var result = new ObjectResult(problemDetails)
86110
{
@@ -90,19 +114,25 @@ public static ObjectResult Forbidden(HttpContext httpContext, System.Exception e
90114
return result;
91115
}
92116

93-
public static ObjectResult NotFound(HttpContext httpContext, System.Exception exc)
117+
public static ObjectResult NotFound(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
94118
{
95119
var statusCode = StatusCodes.Status404NotFound;
96120
var problemDetails = new CustomProblemDetails
97121
{
98122
Status = statusCode,
123+
Detail = exc.Message,
99124
Type = $"https://httpstatuses.com/{statusCode}",
100125
Instance = httpContext.Request.Path,
101126
Title = "NotFound"
102127
};
103128

104129
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
105-
problemDetails.Extensions.Add("errors", exc.Message);
130+
//problemDetails.Extensions.Add("errors", exc.Message);
131+
132+
if (validationError?.Any() ?? false)
133+
{
134+
problemDetails.Extensions.Add("errors", validationError);
135+
}
106136

107137
var result = new ObjectResult(problemDetails)
108138
{
@@ -112,19 +142,25 @@ public static ObjectResult NotFound(HttpContext httpContext, System.Exception ex
112142
return result;
113143
}
114144

115-
public static ObjectResult MethodNotAllowed(HttpContext httpContext, System.Exception exc)
145+
public static ObjectResult MethodNotAllowed(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
116146
{
117147
var statusCode = StatusCodes.Status405MethodNotAllowed;
118148
var problemDetails = new CustomProblemDetails
119149
{
120150
Status = statusCode,
151+
Detail = exc.Message,
121152
Type = $"https://httpstatuses.com/{statusCode}",
122153
Instance = httpContext.Request.Path,
123154
Title = "MethodNotAllowed"
124155
};
125156

126157
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
127-
problemDetails.Extensions.Add("errors", exc.Message);
158+
//problemDetails.Extensions.Add("errors", exc.Message);
159+
160+
if (validationError?.Any() ?? false)
161+
{
162+
problemDetails.Extensions.Add("errors", validationError);
163+
}
128164

129165
var result = new ObjectResult(problemDetails)
130166
{
@@ -134,19 +170,25 @@ public static ObjectResult MethodNotAllowed(HttpContext httpContext, System.Exce
134170
return result;
135171
}
136172

137-
public static ObjectResult NotAcceptable(HttpContext httpContext, System.Exception exc)
173+
public static ObjectResult NotAcceptable(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
138174
{
139175
var statusCode = StatusCodes.Status406NotAcceptable;
140176
var problemDetails = new CustomProblemDetails
141177
{
142178
Status = statusCode,
179+
Detail = exc.Message,
143180
Type = $"https://httpstatuses.com/{statusCode}",
144181
Instance = httpContext.Request.Path,
145182
Title = "NotAcceptable"
146183
};
147184

148185
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
149-
problemDetails.Extensions.Add("errors", exc.Message);
186+
//problemDetails.Extensions.Add("errors", exc.Message);
187+
188+
if (validationError?.Any() ?? false)
189+
{
190+
problemDetails.Extensions.Add("errors", validationError);
191+
}
150192

151193
var result = new ObjectResult(problemDetails)
152194
{
@@ -156,19 +198,25 @@ public static ObjectResult NotAcceptable(HttpContext httpContext, System.Excepti
156198
return result;
157199
}
158200

159-
public static ObjectResult RequestTimeout(HttpContext httpContext, System.Exception exc)
201+
public static ObjectResult RequestTimeout(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
160202
{
161203
var statusCode = StatusCodes.Status408RequestTimeout;
162204
var problemDetails = new CustomProblemDetails
163205
{
164206
Status = statusCode,
207+
Detail = exc.Message,
165208
Type = $"https://httpstatuses.com/{statusCode}",
166209
Instance = httpContext.Request.Path,
167210
Title = "RequestTimeout"
168211
};
169212

170213
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
171-
problemDetails.Extensions.Add("errors", exc.Message);
214+
//problemDetails.Extensions.Add("errors", exc.Message);
215+
216+
if (validationError?.Any() ?? false)
217+
{
218+
problemDetails.Extensions.Add("errors", validationError);
219+
}
172220

173221
var result = new ObjectResult(problemDetails)
174222
{
@@ -178,19 +226,25 @@ public static ObjectResult RequestTimeout(HttpContext httpContext, System.Except
178226
return result;
179227
}
180228

181-
public static ObjectResult Conflict(HttpContext httpContext, System.Exception exc)
229+
public static ObjectResult Conflict(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
182230
{
183231
var statusCode = StatusCodes.Status409Conflict;
184232
var problemDetails = new CustomProblemDetails
185233
{
186234
Status = statusCode,
235+
Detail = exc.Message,
187236
Type = $"https://httpstatuses.com/{statusCode}",
188237
Instance = httpContext.Request.Path,
189238
Title = "Conflict"
190239
};
191240

192241
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
193-
problemDetails.Extensions.Add("errors", exc.Message);
242+
//problemDetails.Extensions.Add("errors", exc.Message);
243+
244+
if (validationError?.Any() ?? false)
245+
{
246+
problemDetails.Extensions.Add("errors", validationError);
247+
}
194248

195249
var result = new ObjectResult(problemDetails)
196250
{
@@ -200,19 +254,53 @@ public static ObjectResult Conflict(HttpContext httpContext, System.Exception ex
200254
return result;
201255
}
202256

203-
public static ObjectResult UnprocessableEntity(HttpContext httpContext, System.Exception exc)
257+
public static ObjectResult UnprocessableEntity(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
204258
{
205259
var statusCode = StatusCodes.Status422UnprocessableEntity;
206260
var problemDetails = new CustomProblemDetails
207261
{
208262
Status = statusCode,
263+
Detail = exc.Message,
209264
Type = $"https://httpstatuses.com/{statusCode}",
210265
Instance = httpContext.Request.Path,
211266
Title = "UnprocessableEntity"
212267
};
213268

214269
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
215-
problemDetails.Extensions.Add("errors", exc.Message);
270+
//problemDetails.Extensions.Add("errors", exc.Message);
271+
272+
if (validationError?.Any() ?? false)
273+
{
274+
problemDetails.Extensions.Add("errors", validationError);
275+
}
276+
277+
var result = new ObjectResult(problemDetails)
278+
{
279+
StatusCode = statusCode
280+
};
281+
282+
return result;
283+
}
284+
285+
public static ObjectResult InternalServerError(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
286+
{
287+
var statusCode = StatusCodes.Status500InternalServerError;
288+
var problemDetails = new CustomProblemDetails
289+
{
290+
Status = statusCode,
291+
Detail = exc.Message,
292+
Type = $"https://httpstatuses.com/{statusCode}",
293+
Instance = httpContext.Request.Path,
294+
Title = "InternalServerError"
295+
};
296+
297+
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
298+
//problemDetails.Extensions.Add("errors", exc.Message);
299+
300+
if (validationError?.Any() ?? false)
301+
{
302+
problemDetails.Extensions.Add("errors", validationError);
303+
}
216304

217305
var result = new ObjectResult(problemDetails)
218306
{

0 commit comments

Comments
 (0)