示例#1
0
        private static void Handle(ExceptionHandlerContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            ExceptionContext exceptionContext = context.ExceptionContext;
            ExceptionDispatchInfo exceptionInfo = exceptionContext.ExceptionInfo;

            if (exceptionInfo == null)
            {
                throw Error.Argument("context", Resources.TypePropertyMustNotBeNull, typeof(ExceptionContext).Name, "ExceptionInfo");
            }

            CommandHandlerRequest request = exceptionContext.Request;

            if (request == null)
            {
                throw Error.Argument("context", Resources.TypePropertyMustNotBeNull, typeof(ExceptionContext).Name, "Request");
            }

            if (exceptionContext.CatchBlock == ExceptionCatchBlocks.ExceptionFilter)
            {
                // The exception filter stage propagates unhandled exceptions by default (when no filter handles the
                // exception).
                return;
            }

            context.Result = new ResponseMessageResult(request.CreateErrorResponse(exceptionInfo.SourceException));
        }
示例#2
0
        public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            // For exceptions at the top of the call stack, Result will start out non-null (due to
            // LastChanceExceptionHandler). This class does not force exceptions back to unhandled in such cases, so it
            // will not not trigger the host-level exception processing.
            context.Result = null;
            return TaskHelpers.Completed();
        }
        /// <summary>Calls an exception handler and determines the response handling it, if any.</summary>
        /// <param name="handler">The unhandled exception handler.</param>
        /// <param name="context">The exception context.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>
        /// A task that, when completed, contains the response message to return when the exception is handled, or
        /// <see langword="null"/> when the exception remains unhandled.
        /// </returns>
        public static Task<HandlerResponse> HandleAsync(this IExceptionHandler handler, ExceptionContext context, CancellationToken cancellationToken)
        {
            if (handler == null)
            {
                throw Error.ArgumentNull("handler");
            }

            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            ExceptionHandlerContext handlerContext = new ExceptionHandlerContext(context);
            return HandleAsyncCore(handler, handlerContext, cancellationToken);
        }
示例#4
0
        /// <summary>Calls an exception handler and determines the response handling it, if any.</summary>
        /// <param name="handler">The unhandled exception handler.</param>
        /// <param name="context">The exception context.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>
        /// A task that, when completed, contains the response message to return when the exception is handled, or
        /// <see langword="null"/> when the exception remains unhandled.
        /// </returns>
        public static Task <HandlerResponse> HandleAsync(this IExceptionHandler handler, ExceptionContext context, CancellationToken cancellationToken)
        {
            if (handler == null)
            {
                throw Error.ArgumentNull("handler");
            }

            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            ExceptionHandlerContext handlerContext = new ExceptionHandlerContext(context);

            return(HandleAsyncCore(handler, handlerContext, cancellationToken));
        }
        public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            if (context != null)
            {
                ExceptionContext exceptionContext = context.ExceptionContext;

                ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock;

                if (catchBlock != null && catchBlock.IsTopLevel)
                {
                    context.Result = CreateDefaultLastChanceResult(exceptionContext);
                }
            }

            return this.innerHandler.HandleAsync(context, cancellationToken);
        }
示例#6
0
        public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            if (context != null)
            {
                ExceptionContext exceptionContext = context.ExceptionContext;

                ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock;

                if (catchBlock != null && catchBlock.IsTopLevel)
                {
                    context.Result = CreateDefaultLastChanceResult(exceptionContext);
                }
            }

            return(this.innerHandler.HandleAsync(context, cancellationToken));
        }
示例#7
0
        /// <summary>Determines whether the exception should be handled.</summary>
        /// <param name="context">The exception handler context.</param>
        /// <returns>
        /// <see langword="true"/> if the exception should be handled; otherwise, <see langword="false"/>.
        /// </returns>
        /// <remarks>The default decision is only to handle exceptions caught at top-level catch blocks.</remarks>
        public virtual bool ShouldHandle(ExceptionHandlerContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            ExceptionContext exceptionContext = context.ExceptionContext;

            ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock;

            if (catchBlock == null)
            {
                throw Error.ArgumentNull("context", Resources.TypePropertyMustNotBeNull, typeof(ExceptionContext), "CatchBlock");
            }

            return(catchBlock.IsTopLevel);
        }
示例#8
0
        /// <summary>Determines whether the exception should be handled.</summary>
        /// <param name="context">The exception handler context.</param>
        /// <returns>
        /// <see langword="true"/> if the exception should be handled; otherwise, <see langword="false"/>.
        /// </returns>
        /// <remarks>The default decision is only to handle exceptions caught at top-level catch blocks.</remarks>
        public virtual bool ShouldHandle(ExceptionHandlerContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            ExceptionContext exceptionContext = context.ExceptionContext;

            ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock;

            if (catchBlock == null)
            {
                throw Error.ArgumentNull("context", Resources.TypePropertyMustNotBeNull, typeof(ExceptionContext), "CatchBlock");
            }

            return catchBlock.IsTopLevel;
        }
示例#9
0
        /// <inheritdoc />
        Task IExceptionHandler.HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            ExceptionContext exceptionContext = context.ExceptionContext;

            if (exceptionContext.ExceptionInfo == null)
            {
                throw Error.Argument("context", Resources.TypePropertyMustNotBeNull, typeof(ExceptionContext).Name, "ExceptionInfo");
            }

            if (!this.ShouldHandle(context))
            {
                return(TaskHelpers.Completed());
            }

            return(this.HandleAsync(context, cancellationToken));
        }
示例#10
0
        /// <inheritdoc />
        Task IExceptionHandler.HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            ExceptionContext exceptionContext = context.ExceptionContext;

            if (exceptionContext.ExceptionInfo == null)
            {
                throw Error.Argument("context", Resources.TypePropertyMustNotBeNull, typeof(ExceptionContext).Name, "ExceptionInfo");
            }

            if (!this.ShouldHandle(context))
            {
                return TaskHelpers.Completed();
            }

            return this.HandleAsync(context, cancellationToken);
        }
示例#11
0
        private static async Task<HandlerResponse> HandleAsyncCore(IExceptionHandler handler, ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            Contract.Assert(handler != null);
            Contract.Assert(context != null);

            await handler.HandleAsync(context, cancellationToken);

            ICommandHandlerResult result = context.Result;

            if (result == null)
            {
                return null;
            }

            HandlerResponse response = await result.ExecuteAsync(cancellationToken);

            if (response == null)
            {
                throw Error.ArgumentNull(Resources.TypeMethodMustNotReturnNull, typeof(ICommandHandlerResult).Name, "ExecuteAsync");
            }

            return response;
        }
示例#12
0
 public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
 {
     Handle(context);
     return(TaskHelpers.Completed());
 }
示例#13
0
 public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
 {
     Handle(context);
     return TaskHelpers.Completed();
 }
示例#14
0
        private static async Task <HandlerResponse> HandleAsyncCore(IExceptionHandler handler, ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            Contract.Assert(handler != null);
            Contract.Assert(context != null);

            await handler.HandleAsync(context, cancellationToken);

            ICommandHandlerResult result = context.Result;

            if (result == null)
            {
                return(null);
            }

            HandlerResponse response = await result.ExecuteAsync(cancellationToken);

            if (response == null)
            {
                throw Error.ArgumentNull(Resources.TypeMethodMustNotReturnNull, typeof(ICommandHandlerResult).Name, "ExecuteAsync");
            }

            return(response);
        }
示例#15
0
 /// <summary>When overridden in a derived class, handles the exception synchronously.</summary>
 /// <param name="context">The exception handler context.</param>
 public virtual void Handle(ExceptionHandlerContext context)
 {
 }
示例#16
0
 /// <summary>When overridden in a derived class, handles the exception synchronously.</summary>
 /// <param name="context">The exception handler context.</param>
 public virtual void Handle(ExceptionHandlerContext context)
 {
 }