private Task HandleExceptionAsync(HttpContext context, Exception ex, ApiExceptionOptions options)
        {
            (HttpStatusCode httpStatusCode, string title) = ex.ToHttpStatusCodeAndTitle();

            var error = new ApiError
            {
                Id     = Guid.NewGuid().ToString(),
                Status = (short)httpStatusCode,
                Title  = title,
            };

            options.AddResponseDetails?.Invoke(context, ex, error);

            string message = GetInnermostExceptionMessage(ex);

            LogLevel level = _options.DetermineLogLevel?.Invoke(ex) ?? LogLevel.Error;

            _logger.Log(level, ex, $"{ExceptionConstants.AnErrorOccurred}: {message} -- {error.Id}.");

            string result = JsonConvert.SerializeObject(error);

            context.Response.ContentType = ApiConstants.ApplicationJson;
            context.Response.StatusCode  = (int)httpStatusCode;

            return(context.Response.WriteAsync(result));
        }
        public static IApplicationBuilder UseApiExceptionHandler(this IApplicationBuilder builder, Action <ApiExceptionOptions> configureOptions)
        {
            var options = new ApiExceptionOptions();

            configureOptions(options);

            return(builder.UseMiddleware <ApiExceptionMiddleware>(options));
        }
 public ApiExceptionMiddleware(ApiExceptionOptions options, RequestDelegate next, ILogger <ApiExceptionMiddleware> logger)
 {
     _next    = next;
     _logger  = logger;
     _options = options;
 }