private static async Task WriteResponse(HttpContext httpContext, bool includeDetails) { var exceptionDetails = httpContext.Features.Get <IExceptionHandlerFeature>(); var ex = exceptionDetails?.Error; if (ex != null) { httpContext.Response.ContentType = "application/problem+json"; var problem = ex switch { ValidationException validationException => CreateValidationProblemDetails(validationException .ToDictionary()), DomainException domainException => CreateProblemDetails(domainException.StatusCode, $"A domain error occured: {domainException.Context}", domainException.Message), _ => CreateProblemDetails(httpContext.Response.StatusCode, includeDetails ? "An error occured: " + ex.Message : "An error occured", includeDetails ? ex.ToString() : null) }; problem.Extensions["traceId"] = Activity.Current?.Id ?? httpContext.TraceIdentifier; httpContext.Response.StatusCode = problem.Status ?? StatusCodes.Status500InternalServerError; await JsonSerializer.SerializeAsync <object>(httpContext.Response.Body, problem); } }