Ejemplo n.º 1
0
        public virtual Task ExecuteRequest(ICommand command, IEnumerable <string> arguments, IEnumerable <IParameter> parameters, CancellationToken cancellationToken)
        {
            return(exceptionHandler.TryAsync(arguments, args =>
            {
                var firstArgument = arguments.FirstOrDefault();
                bool hasFirstArgument = false;

                if (RequiresArguments && (hasFirstArgument = firstArgument == null))
                {
                    throw ModuleException("Invalid arguments", LogLevel.Warning);
                }

                var remainingArguments = hasFirstArgument ? arguments.RemoveAt(0) : arguments;

                if (ActionDictionary.TryGetValue(firstArgument ?? command.Name.ToLower(), out var action))
                {
                    action(remainingArguments, parameters, cancellationToken);
                }
                else
                {
                    return DefaultAction?.Invoke(firstArgument, remainingArguments, parameters, cancellationToken);
                }

                return Task.CompletedTask;
            }, exception =>
            {
                if (exception is ModuleException moduleException)
                {
                    return WriteLineAsyncAction(exception.Message, moduleException.Arguments, moduleException.LogLevel);
                }

                return WriteLineAsyncAction(exception.Message, null, LogLevel.Error);
            },
                                             exceptionTypes => exceptionTypes
                                             .DescribeType <ModuleException>()
                                             .DescribeType <DataValidationException>()));
        }