Ejemplo n.º 1
0
        public void CanGetTextualRepresentationForExceptionWithEmptyValidationResults()
        {
            var exception = new ArgumentValidationException(new ValidationResults(), "param");
            var toString  = exception.ToString();

            Assert.IsNotNull(toString);
        }
        public void CanDeserializeSerializedException()
        {
            var results = new ValidationResults();

            results.AddResult(new ValidationResult("message1", null, null, null, null));
            results.AddResult(new ValidationResult("message2", null, "the key", null, null));
            results.AddResult(new ValidationResult("message3", null, null, null, null));
            var exception = new ArgumentValidationException(results, "param");

            var formatter = new BinaryFormatter();
            ArgumentValidationException deserializedException;

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, exception);
                stream.Seek(0L, SeekOrigin.Begin);
                deserializedException = (ArgumentValidationException)formatter.Deserialize(stream);
            }

            var toString = deserializedException.ToString();

            Assert.IsNotNull(toString);
            Assert.IsTrue(toString.Contains("message1"));
            Assert.IsTrue(toString.Contains("message2"));
            Assert.IsTrue(toString.Contains("message3"));
        }
Ejemplo n.º 3
0
        public void CanGetTextualRepresentationForExceptionWithKeylessValidationResults()
        {
            var results = new ValidationResults();

            results.AddResult(new ValidationResult("message1", null, null, null, null));
            var exception = new ArgumentValidationException(results, "param");
            var toString  = exception.ToString();

            Assert.IsNotNull(toString);
            Assert.IsTrue(toString.Contains("message1"));
        }
            public Exception HandleException(Exception exception, Guid handlingInstanceId)
            {
                ArgumentValidationException ae = exception as ArgumentValidationException;

                if (ae == null)
                {
                    return(exception);
                }

                StringBuilder builder = new StringBuilder();

                builder.Append("Data validation error.\nID = ");
                builder.Append(handlingInstanceId);
                builder.AppendLine();
                builder.Append(exception.Message);
                builder.AppendLine();
                foreach (var validationResult in ae.ValidationResults)
                {
                    builder.Append(validationResult.Message);
                }

                return(new ServiceValidationException(builder.ToString()));
            }
Ejemplo n.º 5
0
        /// <summary>
        /// Executes the specified command with given parameters
        /// </summary>
        /// <param name="args">Command line arguments</param>
        /// <returns>If target method returns int, this method will return that value. Else,
        /// it will return 0 in case of success and 1 in case of unhandled exception</returns>
        public int Run(params string[] args)
        {
            try
            {
                AppCreator appCreator = new AppCreator(_settings);

                CommandLineApplication app = appCreator.CreateApplication(typeof(T), DependencyResolver);

                return(app.Execute(_parserBuilder.Build(), args));
            }
            catch (AppRunnerException e)
            {
                _settings.Error.WriteLine(e.Message + "\n");
#if DEBUG
                _settings.Error.WriteLine(e.StackTrace);
#endif
                return(1);
            }
            catch (CommandParsingException e)
            {
                var optionHelp = e.Command.OptionHelp;
                if (optionHelp != null)
                {
                    _settings.Out.WriteLine(
                        $"Specify --{optionHelp.LongName} for a list of available options and commands.");
                }

                _settings.Error.WriteLine(e.Message + "\n");
                e.Command.ShowHelp();

#if DEBUG
                _settings.Error.WriteLine(e.StackTrace);
#endif

                return(1);
            }
            catch (ValueParsingException e)
            {
                _settings.Error.WriteLine(e.Message + "\n");
                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x.GetBaseException() is AppRunnerException) ||
                                              e.InnerExceptions.Any(x =>
                                                                    x.GetBaseException() is CommandParsingException))
            {
                foreach (var innerException in e.InnerExceptions)
                {
                    _settings.Error.WriteLine(innerException.GetBaseException().Message + "\n");
#if DEBUG
                    _settings.Error.WriteLine(innerException.GetBaseException().StackTrace);
                    if (e.InnerExceptions.Count > 1)
                    {
                        _settings.Error.WriteLine("-----------------------------------------------------------------");
                    }
#endif
                }

                return(1);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x =>
                                                                    x.GetBaseException() is ArgumentValidationException))

            {
                ArgumentValidationException validationException =
                    (ArgumentValidationException)e.InnerExceptions.FirstOrDefault(x =>
                                                                                  x.GetBaseException() is ArgumentValidationException);

                foreach (var failure in validationException.ValidationResult.Errors)
                {
                    _settings.Out.WriteLine(failure.ErrorMessage);
                }

                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x.GetBaseException() is ValueParsingException)
                                              )

            {
                ValueParsingException valueParsingException =
                    (ValueParsingException)e.InnerExceptions.FirstOrDefault(x =>
                                                                            x.GetBaseException() is ValueParsingException);

                _settings.Error.WriteLine(valueParsingException.Message + "\n");

                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x is TargetInvocationException))
            {
                TargetInvocationException ex =
                    (TargetInvocationException)e.InnerExceptions.SingleOrDefault(x => x is TargetInvocationException);

                ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
                return(1); // this will never be called
            }
            catch (AggregateException e)
            {
                foreach (Exception innerException in e.InnerExceptions)
                {
                    ExceptionDispatchInfo.Capture(innerException).Throw();
                }

                return(1); // this will never be called if there is any inner exception
            }
            catch (ArgumentValidationException ex)
            {
                foreach (var failure in ex.ValidationResult.Errors)
                {
                    _settings.Out.WriteLine(failure.ErrorMessage);
                }

                return(2);
            }
            catch (TargetInvocationException ex)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
                return(1); // this will never be called
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Exceutes the specified command with given parameters
        /// </summary>
        /// <param name="args">Command line arguments</param>
        /// <returns>If target method returns int, this method will return that value. Else,
        /// it will return 0 in case of successs and 1 in case of unhandled exception</returns>
        public int Run(params string[] args)
        {
            try
            {
                AppCreator appCreator = new AppCreator(_settings);

                CommandLineApplication app = appCreator.CreateApplication(typeof(T), DependencyResolver);

                var parsedArguments = ArgumentParser.SplitFlags(args).ToArray();

                return(app.Execute(parsedArguments));
            }
            catch (AppRunnerException e)
            {
                Console.Error.WriteLine(e.Message + "\n");
#if DEBUG
                Console.Error.WriteLine(e.StackTrace);
#endif
                return(1);
            }
            catch (CommandParsingException e)
            {
                Console.Error.WriteLine(e.Message + "\n");
                e.Command.ShowHelp();

#if DEBUG
                Console.Error.WriteLine(e.StackTrace);
#endif

                return(1);
            }
            catch (ValueParsingException e)
            {
                Console.Error.WriteLine(e.Message + "\n");
                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x.GetBaseException() is AppRunnerException) ||
                                              e.InnerExceptions.Any(x => x.GetBaseException() is CommandParsingException))
            {
                foreach (var innerException in e.InnerExceptions)
                {
                    Console.Error.WriteLine(innerException.GetBaseException().Message + "\n");
#if DEBUG
                    Console.Error.WriteLine(innerException.GetBaseException().StackTrace);
                    if (e.InnerExceptions.Count > 1)
                    {
                        Console.Error.WriteLine("-----------------------------------------------------------------");
                    }
#endif
                }

                return(1);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x.GetBaseException() is ArgumentValidationException))

            {
                ArgumentValidationException validationException =
                    (ArgumentValidationException)e.InnerExceptions.FirstOrDefault(x => x.GetBaseException() is ArgumentValidationException);

                foreach (var failure in validationException.ValidationResult.Errors)
                {
                    Console.WriteLine(failure.ErrorMessage);
                }

                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x.GetBaseException() is ValueParsingException))

            {
                ValueParsingException valueParsingException =
                    (ValueParsingException)e.InnerExceptions.FirstOrDefault(x => x.GetBaseException() is ValueParsingException);

                Console.Error.WriteLine(valueParsingException.Message + "\n");

                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x is TargetInvocationException))
            {
                TargetInvocationException ex =
                    (TargetInvocationException)e.InnerExceptions.SingleOrDefault(x => x is TargetInvocationException);

                ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
                return(1); // this will never be called
            }
            catch (AggregateException e)
            {
                foreach (Exception innerException in e.InnerExceptions)
                {
                    ExceptionDispatchInfo.Capture(innerException).Throw();
                }

                return(1); // this will never be called if there is any inner exception
            }
            catch (ArgumentValidationException ex)
            {
                foreach (var failure in ex.ValidationResult.Errors)
                {
                    Console.WriteLine(failure.ErrorMessage);
                }

                return(2);
            }
            catch (TargetInvocationException ex)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
                return(1); // this will never be called
            }
        }