using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace ImageConvertService.ExceptionHandling;

public class UserFriendlyExceptionFilter : IExceptionFilter
{
    public void OnException(ExceptionContext context)
    {
        if (context.Exception is UserFriendlyException userFriendlyException)
        {
            // 设置处理状态为已处理，阻止默认的异常处理逻辑
            context.ExceptionHandled = true;

            // 设置响应状态码和内容
            context.Result = new ObjectResult(new { error = userFriendlyException.Message })
            {
                StatusCode = 400 // Bad Request
            };
        }
    }
}