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

Commit

Permalink
Merge pull request #28 from AngeloDotNet/5-status-code-management-501
Browse files Browse the repository at this point in the history
5 status code management 501
  • Loading branch information
AngeloDotNet authored Oct 24, 2023
2 parents 8da7f06 + b0fa742 commit 6ffb8a2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or
| 409 | ConflictException | available |
| 422 | UnprocessableEntityException | available |
| 500 | InternalServerErrorException | available |
| 501 | NotImplementedException | coming soon |
| 501 | NotImplementedException | available |
| 502 | BadGatewayException | coming soon |
| 503 | ServiceUnavailableException | coming soon |
| 504 | GatewayTimeoutException | coming soon |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,18 @@ public async Task<IActionResult> GetExceptionInternalServerErrorAsync()
return ResponseException.InternalServerError(HttpContext, exc);
}
}

[HttpGet("NotImplemented")]
public async Task<IActionResult> GetExceptionNotImplementedAsync()
{
try
{
await Task.Delay(500);
throw new Exception.NotImplementedException("Not Implemented");
}
catch (Exception.NotImplementedException exc)
{
return ResponseException.NotImplemented(HttpContext, exc);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CustomLibrary.ProblemDetails.Exception;

public class NotImplementedException : System.Exception
{
public NotImplementedException()
{
}

public NotImplementedException(string message) : base(message)
{
}

public NotImplementedException(string message, System.Exception innerException) : base(message, innerException)
{
}
}
28 changes: 28 additions & 0 deletions src/CustomLibrary.ProblemDetails/ResponseException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,32 @@ public static ObjectResult InternalServerError(HttpContext httpContext, System.E

return result;
}

public static ObjectResult NotImplemented(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
{
var statusCode = StatusCodes.Status501NotImplemented;
var problemDetails = new CustomProblemDetails
{
Status = statusCode,
Detail = exc.Message,
Type = $"https://httpstatuses.com/{statusCode}",
Instance = httpContext.Request.Path,
Title = "InternalServerError"
};

problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
//problemDetails.Extensions.Add("errors", exc.Message);

if (validationError?.Any() ?? false)
{
problemDetails.Extensions.Add("errors", validationError);
}

var result = new ObjectResult(problemDetails)
{
StatusCode = statusCode
};

return result;
}
}

0 comments on commit 6ffb8a2

Please sign in to comment.