Files
wehub-resource-sync b957a53def
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:21:23 +08:00

37 lines
1.1 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using ContentSafety.Exceptions;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
namespace ContentSafety.Handlers;
/// <summary>
/// Exception handler for content safety scenarios.
/// It allows to return formatted content back to the client with exception details.
/// </summary>
public class ContentSafetyExceptionHandler : IExceptionHandler
{
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
if (exception is not TextModerationException and not AttackDetectionException)
{
return false;
}
var problemDetails = new ProblemDetails
{
Status = StatusCodes.Status400BadRequest,
Title = "Bad Request",
Detail = exception.Message,
Extensions = (IDictionary<string, object?>)exception.Data
};
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
return true;
}
}