-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjusted to the latest the greatest .NET 8 improvements around Proble…
…m Details
- Loading branch information
1 parent
dd3f9f7
commit 1bf4f4d
Showing
18 changed files
with
202 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...erine/Helpdesk.Api/Core/Http/Middlewares/ExceptionHandling/ExceptionHandlingMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Helpdesk.Api.Core.Http.Middlewares.ExceptionHandling; | ||
|
||
public class ExceptionToProblemDetailsHandler(Func<Exception, HttpContext, ProblemDetails?>? customExceptionMap) | ||
: IExceptionHandler | ||
{ | ||
public async ValueTask<bool> TryHandleAsync( | ||
HttpContext httpContext, | ||
Exception exception, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var details = customExceptionMap?.Invoke(exception, httpContext) ?? exception.MapToProblemDetails(); | ||
|
||
httpContext.Response.StatusCode = details.Status ?? StatusCodes.Status500InternalServerError; | ||
await httpContext.Response | ||
.WriteAsJsonAsync(details, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
public static class ExceptionHandlingMiddleware | ||
{ | ||
public static IServiceCollection AddDefaultExceptionHandler( | ||
this IServiceCollection serviceCollection, | ||
Func<Exception, HttpContext, ProblemDetails?>? customExceptionMap = null | ||
) => | ||
serviceCollection | ||
.AddProblemDetails() | ||
.AddSingleton<IExceptionHandler>(new ExceptionToProblemDetailsHandler(customExceptionMap)); | ||
} | ||
|
||
public static class ProblemDetailsExtensions | ||
{ | ||
public static ProblemDetails MapToProblemDetails(this Exception exception) | ||
{ | ||
var statusCode = exception switch | ||
{ | ||
ArgumentException _ => StatusCodes.Status400BadRequest, | ||
ValidationException _ => StatusCodes.Status400BadRequest, | ||
UnauthorizedAccessException _ => StatusCodes.Status401Unauthorized, | ||
InvalidOperationException _ => StatusCodes.Status403Forbidden, | ||
NotImplementedException _ => StatusCodes.Status501NotImplemented, | ||
_ => StatusCodes.Status500InternalServerError | ||
}; | ||
|
||
return exception.MapToProblemDetails(statusCode); | ||
} | ||
|
||
public static ProblemDetails MapToProblemDetails( | ||
this Exception exception, | ||
int statusCode, | ||
string? title = null, | ||
string? detail = null | ||
) => | ||
new() { Title = title ?? exception.GetType().Name, Detail = detail ?? exception.Message, Status = statusCode }; | ||
} |
Oops, something went wrong.