feat: new exceptions
This commit is contained in:
@@ -5,7 +5,11 @@ using Microsoft.AspNetCore.Mvc;
|
||||
namespace MemeVault.Api.Middleware;
|
||||
|
||||
/// <summary>
|
||||
/// Конвертирует DomainException в 400 Problem Details.
|
||||
/// Конвертирует DomainException и наследников в Problem Details:
|
||||
/// <list type="bullet">
|
||||
/// <item><see cref="InsufficientPermissionException"/> → 403</item>
|
||||
/// <item>Все остальные <see cref="DomainException"/> → 400</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
public sealed class DomainExceptionHandler : IExceptionHandler
|
||||
{
|
||||
@@ -17,14 +21,18 @@ public sealed class DomainExceptionHandler : IExceptionHandler
|
||||
if (ex is not DomainException domain)
|
||||
return false;
|
||||
|
||||
var status = domain is InsufficientPermissionException
|
||||
? StatusCodes.Status403Forbidden
|
||||
: StatusCodes.Status400BadRequest;
|
||||
|
||||
var problem = new ProblemDetails
|
||||
{
|
||||
Title = "Business rule violation",
|
||||
Title = domain is InsufficientPermissionException ? "Forbidden" : "Business rule violation",
|
||||
Detail = domain.Message,
|
||||
Status = StatusCodes.Status400BadRequest,
|
||||
Status = status,
|
||||
};
|
||||
|
||||
ctx.Response.StatusCode = problem.Status!.Value;
|
||||
ctx.Response.StatusCode = status;
|
||||
await ctx.Response.WriteAsJsonAsync(problem, ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user