/// <summary>
 /// Sets the HTTP exception handler on an <see cref="IWebModule" />.
 /// </summary>
 /// <typeparam name="TWebModule">The type of the web server.</typeparam>
 /// <param name="this">The <typeparamref name="TWebModule" /> on which this method is called.</param>
 /// <param name="handler">The HTTP exception handler.</param>
 /// <returns><paramref name="this"/> with the <see cref="IWebModule.OnHttpException">OnHttpException</see>
 /// property set to <paramref name="handler" />.</returns>
 /// <exception cref="NullReferenceException"><paramref name="this" /> is <see langword="null" />.</exception>
 /// <exception cref="InvalidOperationException">The module's configuration is locked.</exception>
 /// <seealso cref="IWebModule.OnHttpException" />
 /// <seealso cref="HttpExceptionHandler" />
 public static TWebModule HandleHttpException <TWebModule>(this TWebModule @this, HttpExceptionHandlerCallback handler)
     where TWebModule : IWebModule
 {
     @this.OnHttpException = handler;
     return(@this);
 }
Exemple #2
0
        internal static async Task Handle(string logSource, IHttpContext context, Exception exception, HttpExceptionHandlerCallback handler)
        {
            if (handler == null || !(exception is IHttpException httpException))
            {
                ExceptionDispatchInfo.Capture(exception).Throw();
                return;
            }

            exception.Log(logSource, $"[{context.Id}] HTTP exception {httpException.StatusCode}");

            try
            {
                context.Response.SetEmptyResponse(httpException.StatusCode);
                context.Response.DisableCaching();
                httpException.PrepareResponse(context);
                await handler(context, httpException)
                .ConfigureAwait(false);
            }
            catch (OperationCanceledException) when(context.CancellationToken.IsCancellationRequested)
            {
                throw;
            }
            catch (HttpListenerException)
            {
                throw;
            }
            catch (Exception exception2)
            {
                exception2.Log(logSource, $"[{context.Id}] Unhandled exception while handling HTTP exception {httpException.StatusCode}");
            }
        }
Exemple #3
0
 /// <summary>
 /// Sets the HTTP exception handler on a <see cref="IWebServer" />.
 /// </summary>
 /// <typeparam name="TWebServer">The type of the web server.</typeparam>
 /// <param name="this">The <typeparamref name="TWebServer" /> on which this method is called.</param>
 /// <param name="handler">The HTTP exception handler.</param>
 /// <returns><paramref name="this"/> with the <see cref="IWebServer.OnHttpException">OnHttpException</see>
 /// property set to <paramref name="handler" />.</returns>
 /// <exception cref="NullReferenceException"><paramref name="this" /> is <see langword="null" />.</exception>
 /// <exception cref="InvalidOperationException">The web server has already been started.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="handler" /> is <see langwrd="null" />.</exception>
 /// <seealso cref="IWebServer.OnHttpException" />
 /// <seealso cref="HttpExceptionHandler" />
 public static TWebServer HandleHttpException <TWebServer>(this TWebServer @this, HttpExceptionHandlerCallback handler)
     where TWebServer : IWebServer
 {
     @this.OnHttpException = handler;
     return(@this);
 }