public InternalExceptionHandlerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions <InternalExceptionHandlerOptions> options)
        {
            _next    = next;
            _logger  = loggerFactory.CreateLogger <InternalExceptionHandlerMiddleware>();
            _options = options.Value;
            _clearCacheHeadersDelegate = ClearCacheHeaders;
            if (_options.ExceptionHandler != null)
            {
                return;
            }
            if (_options.ExceptionHandlingPath == null)
            {
                throw new InvalidOperationException($"{nameof(_options.ExceptionHandlingPath)} is null");
            }

            _options.ExceptionHandler = _next;
        }
        /// <summary>
        /// Adds a middleware to the pipeline that will catch pivotal internal exceptions, log them , set the <see cref="InternalExceptionHandlerFeature"/>
        /// The request will not be re-executed if the response has already started.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseInternalExceptionHandler(this IApplicationBuilder app, InternalExceptionHandlerOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(app.UseMiddleware <InternalExceptionHandlerMiddleware>(Options.Create(options)));
        }