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

Commit 6ffb8a2

Browse files
authored
Merge pull request #28 from AngeloDotNet/5-status-code-management-501
5 status code management 501
2 parents 8da7f06 + b0fa742 commit 6ffb8a2

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or
2525
| 409 | ConflictException | available |
2626
| 422 | UnprocessableEntityException | available |
2727
| 500 | InternalServerErrorException | available |
28-
| 501 | NotImplementedException | coming soon |
28+
| 501 | NotImplementedException | available |
2929
| 502 | BadGatewayException | coming soon |
3030
| 503 | ServiceUnavailableException | coming soon |
3131
| 504 | GatewayTimeoutException | coming soon |

src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,18 @@ public async Task<IActionResult> GetExceptionInternalServerErrorAsync()
165165
return ResponseException.InternalServerError(HttpContext, exc);
166166
}
167167
}
168+
169+
[HttpGet("NotImplemented")]
170+
public async Task<IActionResult> GetExceptionNotImplementedAsync()
171+
{
172+
try
173+
{
174+
await Task.Delay(500);
175+
throw new Exception.NotImplementedException("Not Implemented");
176+
}
177+
catch (Exception.NotImplementedException exc)
178+
{
179+
return ResponseException.NotImplemented(HttpContext, exc);
180+
}
181+
}
168182
}
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 NotImplementedException : System.Exception
4+
{
5+
public NotImplementedException()
6+
{
7+
}
8+
9+
public NotImplementedException(string message) : base(message)
10+
{
11+
}
12+
13+
public NotImplementedException(string message, System.Exception innerException) : base(message, innerException)
14+
{
15+
}
16+
}

src/CustomLibrary.ProblemDetails/ResponseException.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,32 @@ public static ObjectResult InternalServerError(HttpContext httpContext, System.E
309309

310310
return result;
311311
}
312+
313+
public static ObjectResult NotImplemented(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
314+
{
315+
var statusCode = StatusCodes.Status501NotImplemented;
316+
var problemDetails = new CustomProblemDetails
317+
{
318+
Status = statusCode,
319+
Detail = exc.Message,
320+
Type = $"https://httpstatuses.com/{statusCode}",
321+
Instance = httpContext.Request.Path,
322+
Title = "InternalServerError"
323+
};
324+
325+
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
326+
//problemDetails.Extensions.Add("errors", exc.Message);
327+
328+
if (validationError?.Any() ?? false)
329+
{
330+
problemDetails.Extensions.Add("errors", validationError);
331+
}
332+
333+
var result = new ObjectResult(problemDetails)
334+
{
335+
StatusCode = statusCode
336+
};
337+
338+
return result;
339+
}
312340
}

0 commit comments

Comments
 (0)