Exemplo n.º 1
0
        /// <summary>
        /// Takes care of any possible error that can happen in the ActionHandler and it prepares the EventResult.
        /// </summary>
        /// <param name="event"></param>
        /// <returns></returns>
        protected virtual async Task <EventResult> ProcessWithActionHandler(Event @event)
        {
            EventResult eventResult;

            try
            {
                eventResult = await ActionHandler.Process(@event);

                if (eventResult == null)
                {
                    /*extreme case where the action handler is not implemented properly*/
                    eventResult = new EventResult
                    {
                        Exception = new ExceptionResult(
                            "ActionHandler has returned null as the EventResult. Please correct the ActionHandler in order to always return a valid EventResult"),
                        Response      = null,
                        OriginalEvent = @event
                    };
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogError(Logger,
                                   LogMessage.GetLogMessage(
                                       "Handled Exception. ActionHandler returned an exception. This should be always avoided!: " +
                                       exception, GetType()));

                /*
                 * Another case where the ActionHandler simply doesn't work properly and it throws an error.
                 * ActionHandler should always handle the error message inside the Process method, but this obviously
                 * sometimes might not work!
                 * */
                eventResult = new EventResult()
                {
                    Exception     = new ExceptionResult(exception),
                    Response      = null,
                    OriginalEvent = @event
                };
            }

            return(eventResult);
        }