// Copyright (c) Microsoft. All rights reserved. using ContentSafety.Exceptions; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; namespace ContentSafety.Handlers; /// /// Exception handler for content safety scenarios. /// It allows to return formatted content back to the client with exception details. /// public class ContentSafetyExceptionHandler : IExceptionHandler { public async ValueTask 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)exception.Data }; httpContext.Response.StatusCode = StatusCodes.Status400BadRequest; await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken); return true; } }