/// <summary> /// Register common exception handler chain which is invoked before the ones registered to exception type. /// </summary> /// <param name="configure">An <see cref="Action{IExceptionHandlerBuilder}"/> to build the exception handler chain.</param> /// <returns>The current <see cref="IExceptionPolicyBuilder"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="configure"/> is null.</exception> public IExceptionPolicyBuilder Pre(Action <IExceptionHandlerBuilder> configure) { Guard.ArgumentNotNull(configure, nameof(configure)); ExceptionHandlerBuilder builder = new ExceptionHandlerBuilder(this.ServiceProvider); configure(builder); _preHanlderBuilder.Use(async context => { await builder.Build()(context); }); return(this); }
public void AddPreHandlers(Func <Exception, bool> predicate, Action <IExceptionHandlerBuilder> configure) { Guard.ArgumentNotNull(predicate, nameof(predicate)); Guard.ArgumentNotNull(configure, nameof(configure)); ExceptionHandlerBuilder builder = new ExceptionHandlerBuilder(this.ServiceProvider); configure(builder); _postHanlderBuilder.AddHandler(async context => { if (predicate(context.Exception)) { await builder.Build()(context); } }); }
/// <summary> /// Register common exception handler chain which is invoked before the ones registered to exception type. /// </summary> /// <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> /// <exception cref="ArgumentNullException">The <paramref name="predicate"/> is null.</exception> /// <exception cref="ArgumentNullException">The <paramref name="configure"/> is null.</exception> public IExceptionPolicyBuilder Pre(Func <Exception, bool> predicate, Action <IExceptionHandlerBuilder> configure) { Guard.ArgumentNotNull(predicate, nameof(predicate)); Guard.ArgumentNotNull(configure, nameof(configure)); ExceptionHandlerBuilder builder = new ExceptionHandlerBuilder(this.ServiceProvider); configure(builder); _preHanlderBuilder.Use(async context => { if (predicate(context.Exception)) { await builder.Build()(context); } }); return(this); }
/// <summary> /// Register exception handler chain for specified exception type. /// </summary> /// <param name="exceptionType">The type of exception handled by the registered exception handler chain.</param> /// <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> /// <exception cref="ArgumentNullException">The <paramref name="exceptionType"/> is null.</exception> /// <exception cref="ArgumentNullException">The <paramref name="postHandlingAction"/> is null.</exception> /// <exception cref="ArgumentNullException">The <paramref name="configure"/> is null.</exception> public IExceptionPolicyBuilder For(Type exceptionType, PostHandlingAction postHandlingAction, Action <IExceptionHandlerBuilder> configure) { Guard.ArgumentNotNull(exceptionType, nameof(exceptionType)); Guard.ArgumentNotNull(configure, nameof(configure)); if (_policyEntries.Any(it => it.ExceptionType == exceptionType)) { throw new ArgumentException(Resources.ExceptionDuplicateExceptionType.Fill(exceptionType.FullName), nameof(exceptionType)); } ExceptionHandlerBuilder builder = new ExceptionHandlerBuilder(this.ServiceProvider); configure(builder); _policyEntries.Add(new ExceptionPolicyEntry(exceptionType, postHandlingAction, builder.Build())); return(this); }
/// <summary> /// Build the exception policy. /// </summary> /// <returns>The exception policy to build.</returns> public IExceptionPolicy Build() { return(new ExceptionPolicy(_policyEntries, _preHanlderBuilder.Build(), _postHanlderBuilder.Build())); }