using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;


namespace IRaCIS.Core.Application.BusinessFilter;


/// <summary>
/// 不生效，不知道为啥 
/// </summary>
public class GlobalExceptionHandler : IExceptionHandler
{
    private readonly ILogger<GlobalExceptionHandler> _logger;
    public GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger)
    {
        this._logger = logger;
    }
    public ValueTask<bool> TryHandleAsync(
        HttpContext httpContext,
        Exception exception,
        CancellationToken cancellationToken)
    {


        httpContext.Response.ContentType = "application/json";


        var ex = exception;
        var errorInfo = $"Exception: {ex.Message}[{ex.StackTrace}]" + (ex.InnerException != null ? $" InnerException: {ex.InnerException.Message}[{ex.InnerException.StackTrace}]" : "");

        httpContext.Response.WriteAsJsonAsync(ResponseOutput.NotOk($"{ex?.Message}"));





        _logger.LogError(errorInfo);

        // return true to signal that this exception is handled
        return ValueTask.FromResult(false);
    }
}
