Example #1
0
        /// <summary>
        /// Get registered exception filters stored in the specified <see cref="IExceptionManagerBuilder"/>'s property collection.
        /// </summary>
        /// <param name="builder">The <see cref="IExceptionManagerBuilder"/> whose property collection includes the filters to get.</param>
        /// <returns>A <see cref="IDictionary{String, IExceptionFilter}"/> representing the named exception filters.</returns>
        /// <exception cref="ArgumentNullException">The specified <paramref name="builder"/> is null.</exception>
        public static IDictionary <string, IExceptionFilter> GetFilters(this IExceptionManagerBuilder builder)
        {
            var filters = Guard.ArgumentNotNull(builder, nameof(builder)).Properties.TryGetValue(KeyOfFilters, out object value)
                ? value
                : builder.Properties[KeyOfFilters] = new Dictionary <string, IExceptionFilter>(StringComparer.OrdinalIgnoreCase);

            return((IDictionary <string, IExceptionFilter>)filters);
        }
Example #2
0
        public void UseFilter_Arguments_Not_Allow_Null(string builderIndicator, string name, Type type)
        {
            IExceptionManagerBuilder builder = builderIndicator == null
                ? null
                : new ExceptionManagerBuilder(new ServiceCollection().BuildServiceProvider());

            Assert.ThrowsAny <ArgumentException>(() => builder.UseFilter(name, type));
        }
Example #3
0
        /// <summary>
        /// Register an exception filter to specified <see cref="IExceptionManagerBuilder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IExceptionManagerBuilder"/> to which the exception filter is registered.</param>
        /// <param name="filterName">The name of the registered exception filter.</param>
        /// <param name="filterType">The type of exception filter.</param>
        /// <param name="argumnets">The arguments passed to construct the target exception filter.</param>
        /// <returns>The <see cref="IExceptionManagerBuilder"/> with the exception filter registration.</returns>
        /// <exception cref="ArgumentNullException">The specified <paramref name="builder"/> is null.</exception>
        /// <exception cref="ArgumentNullException">The specified <paramref name="filterName"/> is null.</exception>
        /// <exception cref="ArgumentNullException">The specified <paramref name="filterType"/> is null.</exception>
        /// <exception cref="ArgumentException">The specified <paramref name="filterName"/> is a white space string.</exception>
        public static IExceptionManagerBuilder UseFilter(this IExceptionManagerBuilder builder, string filterName, Type filterType, params object[] argumnets)
        {
            Guard.ArgumentNotNull(builder, nameof(builder));
            Guard.ArgumentNotNullOrWhiteSpace(filterName, nameof(filterName));
            Guard.ArgumentAssignableTo <IExceptionFilter>(filterType, nameof(filterType));

            builder.GetFilters()[filterName] = (IExceptionFilter)ActivatorUtilities.CreateInstance(builder.ServiceProvider, filterType, argumnets);
            return(builder);
        }
Example #4
0
        /// <summary>
        /// Load exception policy settings to specified <see cref="IExceptionManagerBuilder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IExceptionManagerBuilder"/> to which the loaded settings is appled.</param>
        /// <param name="filePath">The path of the exception policy configuration file.</param>
        /// <param name="fileProvider">The <see cref="IFileProvider"/> used to load the configuration file.</param>
        /// <returns>The <see cref="IExceptionManagerBuilder"/> to which the loaded settings is appled.</returns>
        /// <remarks>If <paramref name="fileProvider"/> is not explicitly specified, the current directory specific <see cref="PhysicalFileProvider"/> will be used.</remarks>
        /// <exception cref="ArgumentNullException">The specified <paramref name="builder"/> is null.</exception>
        /// <exception cref="ArgumentNullException">The specified <paramref name="filePath"/> is null.</exception>
        /// <exception cref="ArgumentException">The specified <paramref name="filePath"/> is a white space string.</exception>
        public static IExceptionManagerBuilder LoadSettings(this IExceptionManagerBuilder builder, string filePath, IFileProvider fileProvider = null)
        {
            Guard.ArgumentNotNull(builder, nameof(builder));
            Guard.ArgumentNotNullOrWhiteSpace(filePath, nameof(filePath));
            fileProvider = fileProvider ?? new PhysicalFileProvider(Directory.GetCurrentDirectory());

            var configBuilder = new ConfigurationBuilder(fileProvider, filePath);
            var policies      = configBuilder.Build(out IDictionary <string, FilterConfiguration> filters);

            foreach (var it in filters)
            {
                ExceptionFilterConfiguration config = GetFilterConfiguration(it.Value.FilterType);
                config.Use(builder, it.Key, it.Value.Arguments.ToDictionary(item => item.Name, item => item.Value));
            }

            foreach (var it in policies)
            {
                builder.AddPolicy(it.Key, policyBuilder => BuildPolicy(policyBuilder, it.Value, builder.GetFilters()));
            }
            return(builder);
        }
Example #5
0
 /// <summary>
 /// Register the specific exception filter based on configuration.
 /// </summary>
 /// <param name="builder">The <see cref="IExceptionHandlerBuilder"/> used to register the specific exception filter.</param>
 /// <param name="filterName"></param>
 /// <param name="arguments">A <see cref="IDictionary{String, String}"/> storing configuration for the exception filter to register.</param>
 public abstract void Use(IExceptionManagerBuilder builder, string filterName, IDictionary <string, string> arguments);
Example #6
0
        public void GetFilters_Argument_Not_Allow_Null()
        {
            IExceptionManagerBuilder builder = null;

            Assert.Throws <ArgumentNullException>(() => builder.GetFilters());
        }