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 #19 from AngeloDotNet/13-status-code-management-404
Browse files Browse the repository at this point in the history
13 status code management 404
  • Loading branch information
AngeloDotNet authored Aug 16, 2023
2 parents b4a457d + 1e6c3e0 commit 6784eea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ This library is an ad hoc code customization used in my private/work projects th
| 400 | Exception.BadRequestException | Response.BadRequest | available |
| 401 | Exception.UnauthorizedException | Response.Unauthorized | available |
| 403 | Exception.ForbiddenException | Response.Forbidden | available |
| 404 | Exception.NotFoundException | Response.NotFound | coming soon |
| 404 | Exception.NotFoundException | Response.NotFound | available |
| 405 | Exception.NotAllowedException | Response.MethodNotAllowed | coming soon |
| 406 | Exception.NotAcceptableException | Response.NotAcceptable | coming soon |
| 408 | Exception.RequestTimeoutException | Response.RequestTimeout | coming soon |
Expand Down
16 changes: 16 additions & 0 deletions src/CustomLibrary.ProblemDetails/Exception/NotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CustomLibrary.ProblemDetails.Exception;

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

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

public NotFoundException(string message, System.Exception innerException) : base(message, innerException)
{
}
}
22 changes: 22 additions & 0 deletions src/CustomLibrary.ProblemDetails/Response/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,26 @@ public static ObjectResult Forbidden(HttpContext httpContext, System.Exception e

return result;
}

public static ObjectResult NotFound(HttpContext httpContext, System.Exception exc)
{
var statusCode = StatusCodes.Status404NotFound;
var problemDetails = new CustomProblemDetails
{
Status = statusCode,
Type = $"https://httpstatuses.com/{statusCode}",
Instance = httpContext.Request.Path,
Title = "NotFound"
};

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

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

return result;
}
}

0 comments on commit 6784eea

Please sign in to comment.