private static async Task WriteResponse(HttpContext context)
        {
            _context = context;
            var exceptionDetails = context.Features.Get <IExceptionHandlerFeature>();
            var ex = exceptionDetails?.Error;

            if (ex != null)
            {
                context.Response.ContentType = "application/problem+json";
                _problemDetailsFactory       = context?.RequestServices?.GetRequiredService <ProblemDetailsFactory>();

                ProblemDetails problemDetails = ex switch
                {
                    CustomExceptionBase customException => HandleProblemDetails(customException),
                    ValidationExceptionBase validationException => HandleValidationProblemDetails(validationException),
                    _ => HandleDefault(ex),
                };

                var stream = _context.Response.Body;
                if (problemDetails is ValidationProblemDetails validationProblemDetails)
                {
                    await JsonSerializer.SerializeAsync(stream, validationProblemDetails);
                }
                else
                {
                    await JsonSerializer.SerializeAsync(stream, problemDetails);
                }
            }
        }
        private static ValidationProblemDetails HandleValidationProblemDetails(ValidationExceptionBase ex)
        {
            _context.Response.StatusCode = (int)ex.StatusCode;
            var modelStateDictionary = new ModelStateDictionary();

            modelStateDictionary.AddModelError(ex.Property, ex.Message);

            var problemDetails = _problemDetailsFactory.CreateValidationProblemDetails(
                _context,
                statusCode: (int)ex.StatusCode,
                detail: _includeStackTrace ? ex.ToString() : ex.Message,
                modelStateDictionary: modelStateDictionary
                );

            return(problemDetails);
        }