コード例 #1
0
 // next chain
 /// <summary>
 /// Terminates current handlers chain and try to execute next handlers chain which match exception type.
 /// </summary>
 /// <typeparam name="TException">
 /// The exception type
 /// </typeparam>
 /// <param name="builder">
 /// The policy builder
 /// </param>
 /// <param name="index" optional="true">
 /// Handler index in the chain. Optional. By default handler added to the end of chain.
 /// </param>
 /// <returns>
 /// Policy builder
 /// </returns>
 public static IExceptionPolicyBuilder NextPolicy <TException>(
     this IExceptionMapping <TException> builder, int index = -1)
     where TException : Exception
 {
     builder.EnsureHandler <TException, NextChainHandler>(index);
     return(builder);
 }
コード例 #2
0
 public static IExceptionMapping <TException> Clear <TException>(
     this IExceptionMapping <TException> builder)
     where TException : Exception
 {
     builder.Options.Value.ClearHandlers(typeof(TException));
     return(builder);
 }
コード例 #3
0
 // rethrow
 /// <summary>
 /// Re throw exception and stop handlers chain processing.
 /// </summary>
 /// <typeparam name="TException">
 /// The exception type
 /// </typeparam>
 /// <param name="builder">
 /// The policy builder
 /// </param>
 /// <param name="index" optional="true">
 /// Handler index in the chain. Optional. By default handler added to the end of chain.
 /// </param>
 /// <returns>
 /// Policy builder
 /// </returns>
 public static IExceptionPolicyBuilder Rethrow <TException>(
     this IExceptionMapping <TException> builder, int index = -1)
     where TException : Exception
 {
     builder.EnsureHandler <TException, ReThrowExceptionHandler>(index);
     return(builder);
 }
コード例 #4
0
 // mark handled
 /// <summary>
 /// Terminate handlers chain execution and mark exception as "handled" which means that it will not be re thrown.
 /// </summary>
 /// <typeparam name="TException">
 /// The exception type
 /// </typeparam>
 /// <param name="builder">
 /// The policy builder
 /// </param>
 /// <param name="index" optional="true">
 /// Handler index in the chain. Optional. By default handler added to the end of chain.
 /// </param>
 /// <returns>
 /// Policy builder
 /// </returns>
 public static IExceptionPolicyBuilder Handled <TException>(
     this IExceptionMapping <TException> builder, int index = -1)
     where TException : Exception
 {
     builder.EnsureHandler <TException, MarkHandledHandler>(index);
     return(builder);
 }
コード例 #5
0
 public static IExceptionMapping <TException> RemoveHandler <TException, THandler>(
     this IExceptionMapping <TException> builder)
     where THandler : IExceptionHandler
     where TException : Exception
 {
     builder.Options.Value.RemoveHandler(typeof(TException), typeof(THandler));
     return(builder);
 }
コード例 #6
0
        /// <summary>
        /// Disable logging of this particular exception in further handlers for current request.
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <returns>
        /// Policy builder
        /// </returns>
        public static IExceptionMapping <TException> DisableFurtherLog <TException>(
            this IExceptionMapping <TException> builder, int index = -1)
            where TException : Exception
        {
            builder.EnsureHandler <TException, DisableLoggingHandler>(index);

            return(builder);
        }
コード例 #7
0
        // Log
        /// <summary>
        /// Log exception using <see cref="ILoggerFacrory"/> registered in services collection and pass control to next handler in chain
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="settings">
        /// The logs settings. See <see cref="LogHandlerOptions{TException}"/> for details.
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <returns>
        /// Policy builder
        /// </returns>
        public static IExceptionMapping <TException> Log <TException>(
            this IExceptionMapping <TException> builder, Action <LogHandlerOptions <TException> > settings = null, int index = -1)
            where TException : Exception
        {
            builder.Services.Configure <LogHandlerOptions <TException> >(opt => settings?.Invoke(opt));

            builder.EnsureHandler <TException, LogExceptionHandler <TException> >(index);

            return(builder);
        }
コード例 #8
0
        // Set status code
        /// <summary>
        /// Configure response and pass control to next handler. It is recommended to finish chain using <see cref="Handled{TException}"/> when response builder will be configured.
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="responseAlreadyStartedBehaviour">
        /// The begaviour <see cref="ResponseAlreadyStartedBehaviour"/> in case response already started.
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <param name="statusCodeFactory">
        /// The factory for response status code. By default 500 will be used, unless code was already set by another handler for current request.
        /// </param>
        /// <returns>
        /// Response builder.
        /// </returns>
        public static IResponseHandlers <TException> Response <TException>(
            this IExceptionMapping <TException> builder,
            Func <TException, int> statusCodeFactory = null,
            ResponseAlreadyStartedBehaviour responseAlreadyStartedBehaviour = ResponseAlreadyStartedBehaviour.ReThrow,
            int index = -1)
            where TException : Exception
        {
            builder.Services.Configure <RawResponseHandlerOptions <TException> >(responceOptions =>
            {
                responceOptions.ResponseAlreadyStartedBehaviour = responseAlreadyStartedBehaviour;

                responceOptions.SetResponse.Add((context, exception) =>
                {
                    return(RawResponseExceptionHandler <TException> .SetStatusCode(context, exception, statusCodeFactory));
                });
            });

            builder.EnsureHandler <TException, RawResponseExceptionHandler <TException> >(index);

            return(builder as IResponseHandlers <TException>);
        }