Exemplo n.º 1
0
        public static void EnsureHandler <TException, THandler>(
            this IExceptionPolicyBuilder builder, int index = -1)
            where THandler : class, IExceptionHandler
            where TException : Exception

        {
            builder.Options.Value.EnsureHandler(typeof(TException), typeof(THandler), index);
            builder.Services.TryAddSingleton <THandler>();
        }
Exemplo n.º 2
0
        private static void BuildPolicy(IExceptionPolicyBuilder builder, PolicyConfiguration config, IDictionary <string, IExceptionFilter> filters)
        {
            foreach (var it in config.PreHandlers)
            {
                var handlerConfig    = GetHandlerConfiguration(it.HandlerType);
                var filterableConfig = it as FilterableHandlerConfiguration;
                if (null == filterableConfig)
                {
                    builder.Pre(handlerBuilder => handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
                else
                {
                    builder.Pre(handlerBuilder => handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
            }

            foreach (var it in config.PostHandlers)
            {
                var handlerConfig    = GetHandlerConfiguration(it.HandlerType);
                var filterableConfig = it as FilterableHandlerConfiguration;
                if (null == filterableConfig)
                {
                    builder.Post(handlerBuilder => handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
                else
                {
                    builder.Post(handlerBuilder => handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
            }

            foreach (var entry in config.PolicyEntries)
            {
                builder.For(entry.ExceptionType, entry.PostHandlingAction, handlerBuilder => {
                    foreach (var it in entry.Handlers)
                    {
                        var handlerConfig    = GetHandlerConfiguration(it.HandlerType);
                        var filterableConfig = it as FilterableHandlerConfiguration;
                        if (null == filterableConfig)
                        {
                            handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value));
                        }
                        else
                        {
                            handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value));
                        }
                    }
                });
            }
        }
Exemplo n.º 3
0
        public static IExceptionPolicyBuilder BuildEdamosDefault(this IExceptionPolicyBuilder builder)
        {
            builder.For <EdamosApiException>()
            .Log(lo =>
            {
                lo.EventIdFactory = (c, e) => e.EventId;
                lo.Level          = (c, e) => e.LogLevel;
                lo.StateFactory   = (c, e, o) => new FormattedLogValues(
                    $"{e.Message}. Details: {e.DetailsFormat}", e.DetailsParams.ToArray());

                lo.Category = (c, e) => Consts.Logs.EdamosApiCategory;
            })
            .Response(e => (int)e.StatusCode)
            .ClearCacheHeaders()
            .WithObjectResult(
                (r, e) => new EdamosApiError
            {
                StatusCode      = r.HttpContext.Response.StatusCode,
                Message         = e.Message,
                TraceIdentifier = r.HttpContext.TraceIdentifier
            })
            .Handled();

            builder.For <Exception>()
            .Log(lo =>
            {
                lo.Category       = (c, e) => Consts.Logs.EdamosApiCategory;
                lo.Level          = (c, e) => LogLevel.Error;
                lo.EventIdFactory = (c, e) => LogEvents.UnhandledException;
            })
            .Response(null, ResponseAlreadyStartedBehaviour.GoToNextHandler)
            .ClearCacheHeaders()
            .WithObjectResult((r, e) => new EdamosApiError
            {
                StatusCode      = r.HttpContext.Response.StatusCode,
                Message         = "Internal Server Error",
                TraceIdentifier = r.HttpContext.TraceIdentifier
            })
            .Handled();

            return(builder);
        }
 /// <summary>
 /// Register a common exception handler chain.
 /// </summary>
 /// <param name="builder">The <see cref="IExceptionPolicyBuilder"/> to which the common exception handler chain is registered.</param>
 /// <param name="configure">An <see cref="Action{IExceptionHandlerBuilder}"/> to build the exception handler chain.</param>
 /// <returns>The current <see cref="IExceptionPolicyBuilder"/>.</returns>
 public static IExceptionPolicyBuilder Configure(this IExceptionPolicyBuilder builder, Action <IExceptionHandlerBuilder> configure)
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     return(builder.Post(configure));
 }
 /// <summary>
 /// Register exception handler chain for specified exception type.
 /// </summary>
 /// <param name="builder">The <see cref="IExceptionPolicyBuilder"/> to which the type specific hanlder chain is registered.</param>
 /// <typeparam name="TException">The type of exception to handle.</typeparam>
 /// <param name="postHandlingAction">Determining what action should occur after an exception is handled by the configured exception handling chain.</param>
 /// <param name="configure">An <see cref="Action{IExceptionHandlerBuilder}"/> to build the exception handler chain.</param>
 /// <returns>The current <see cref="IExceptionPolicyBuilder"/>.</returns>
 public static IExceptionPolicyBuilder For <TException>(this IExceptionPolicyBuilder builder, PostHandlingAction postHandlingAction, Action <IExceptionHandlerBuilder> configure)
     where TException : Exception
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     return(builder.For(typeof(TException), postHandlingAction, configure));
 }
 internal ExceptionMapping(IExceptionPolicyBuilder builder)
 {
     Builder = builder ?? throw new ArgumentNullException(nameof(builder));
 }
Exemplo n.º 7
0
 public static IExceptionMapping <TException> For <TException>(
     this IExceptionPolicyBuilder builder, int index = -1) where TException : Exception
 {
     builder.Options.EnsureException(typeof(TException), index);
     return(new ExceptionMapping <TException>(builder));
 }
 /// <summary>
 /// Register a common exception handler chain.
 /// </summary>
 /// <param name="builder">The <see cref="IExceptionPolicyBuilder"/> to which the common exception handler chain is registered.</param>
 /// <param name="predicate">A filter used to determine whether the registered exception handler should be invoked.</param>
 /// <param name="configure">An <see cref="Action{IExceptionHandlerBuilder}"/> to build the exception handler chain.</param>
 /// <returns>The current <see cref="IExceptionPolicyBuilder"/>.</returns>
 public static IExceptionPolicyBuilder Use(this IExceptionPolicyBuilder builder, Func <Exception, bool> predicate, Action <IExceptionHandlerBuilder> configure)
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     return(builder.Post(predicate, configure));
 }