/// <summary>
        /// Parses the value with the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="value">The value.</param>
        /// <param name="culture">The culture.</param>
        /// <returns></returns>
        public static object Parse(Type type, string value, CultureInfo culture)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }

            try
            {
                return(TypeDescriptor.GetConverter(type).ConvertFromString(null, culture, value));
            }
            catch (Exception exception)
            {
                ConversionException conversionEx = new ConversionException("An Error Occurred While Casting DB Value", exception);
                conversionEx.Data.Add("VALUE", value);
                conversionEx.Data.Add("TYPE", type.ToString());
                conversionEx.Data.Add("CULTURE", culture.Name);
                throw conversionEx;
            }
        }
예제 #2
0
        public void ConversionException()
        {
            var ex1 = new ConversionException("MyMessage1");
            var ex2 = new ConversionException("MyMessage2", ex1);

            Assert.AreEqual(ex1, ex2.InnerException);
            Assert.AreEqual("MyMessage2", ex2.Message);
        }
        private static ConversionException GetConversionException(object value, Type type, CultureInfo culture, Exception exception = null)
        {
            const string        errorMessage = "An Error Occurred While Changing Value";
            ConversionException conversionEx = exception == null ? new ConversionException(errorMessage) : new ConversionException(errorMessage, exception);

            conversionEx.Data.Add("VALUE", Convert.ToString(value, CultureInfo.CurrentCulture));
            conversionEx.Data.Add("TYPE", type.ToString());
            conversionEx.Data.Add("CULTURE", culture.Name);
            return(conversionEx);
        }
예제 #4
0
        /// <summary>
        /// Gets the conversion exception.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="type">The type.</param>
        /// <param name="culture">The culture.</param>
        /// <param name="exception">The exception.</param>
        /// <returns>ConversionException</returns>
        private static ConversionException GetConversionException(object value, Type type, CultureInfo culture, Exception exception = null)
        {
            string errorMessage = Strings.ConvertUtils_GetConversionException_An_Error_Occurred_While_Changing_Value;
            ConversionException conversionEx = exception == null ? new ConversionException(errorMessage) : new ConversionException(errorMessage, exception);

            conversionEx.Data.Add("VALUE", Convert.ToString(value, CultureInfo.CurrentCulture));
            conversionEx.Data.Add("TYPE", type.ToString());
            conversionEx.Data.Add("CULTURE", culture.Name);
            return(conversionEx);
        }
        public void Test_ConversionException_constructor_value_message()
        {
            // Arrange
            var value   = "abc";
            var message = "Unable to convert value";

            // Act
            var ex = new ConversionException(value, message);

            // Assert
            ex.ShouldNotBeNull();
            value.ShouldBe(ex.Value);
            message.ShouldBe(ex.Message);
            ex.ConvertType.ShouldBeNull();
        }
예제 #6
0
        private static void LogErrorOrWarning(this ILogger logger, TextFile serializedFile, bool isError, string errorMessage, TextSpan errorTextSpan)
        {
            var exception = new ConversionException(serializedFile, null, errorMessage)
            {
                TextSpan = errorTextSpan
            };

            if (isError)
            {
                logger.LogError(exception);
            }
            else
            {
                logger.LogInfo($"{serializedFile}: " + errorMessage);
            }
        }
        public void Test_ConversionException_constructor_value_message_type()
        {
            // Arrange
            var value   = "abc";
            var message = "Unable to convert value";
            var type    = typeof(int);

            // Act
            var ex = new ConversionException(value, message, type);

            // Assert
            ex.ShouldNotBeNull();
            value.ShouldBe(ex.Value);
            message.ShouldBe(ex.Message);
            type.ShouldBe(ex.ConvertType);

            Assert.IsNotNull(ex);
            Assert.AreEqual(value, ex.Value);
            Assert.AreEqual(message, ex.Message);
            Assert.AreEqual(type, ex.ConvertType);
        }
예제 #8
0
        internal Task <bool> RunProcess(
            string args,
            CancellationToken cancellationToken)
        {
            return(Task.Factory.StartNew(() =>
            {
                _outputLog = new List <string>();
                var process = RunProcess(args, FFmpegPath, Priority, true, true, true);
                using (process)
                {
                    process.ErrorDataReceived += (sender, e) => ProcessOutputData(e, args);
                    process.BeginErrorReadLine();
                    // VSTHRD101: Avoid using async lambda for a void returning delegate type, becaues any exceptions not handled by the delegate will crash the process
                    // https://github.com/Microsoft/vs-threading/blob/master/doc/analyzers/VSTHRD101.md
                    var ctr = cancellationToken.Register(() =>
                    {
                        if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                        {
                            try
                            {
                                process.StandardInput.Write("q");
                                Task.Delay(1000 * 5).ConfigureAwait(false).GetAwaiter().GetResult();
                            }
                            catch (InvalidOperationException)
                            {
                            }
                            finally
                            {
                                if (!process.HasExited)
                                {
                                    process.CloseMainWindow();
                                    process.Kill();
                                    _wasKilled = true;
                                }
                            }
                        }
                    });

                    using (ctr)
                    {
                        using (var processEnded = new ManualResetEvent(false))
                        {
                            processEnded.SetSafeWaitHandle(new SafeWaitHandle(process.Handle, false));
                            int index = WaitHandle.WaitAny(new[] { processEnded, cancellationToken.WaitHandle });

                            // If the signal came from the caller cancellation token close the window
                            if (index == 1 &&
                                !process.HasExited)
                            {
                                process.CloseMainWindow();
                                process.Kill();
                                _wasKilled = true;
                            }
                            else if (index == 0 && !process.HasExited)
                            {
                                // Workaround for linux: https://github.com/dotnet/corefx/issues/35544
                                process.WaitForExit();
                            }
                        }

                        cancellationToken.ThrowIfCancellationRequested();
                        if (_wasKilled)
                        {
                            throw new ConversionException("Cannot stop process. Killed it.", args);
                        }

                        if (cancellationToken.IsCancellationRequested)
                        {
                            return false;
                        }

                        List <ExceptionCheck> allExceptions = new List <ExceptionCheck>();
                        string exceptionOutput = string.Join(Environment.NewLine, _outputLog.ToArray());

                        ConversionException conversionException = new ConversionException(exceptionOutput, args);
                        allExceptions.Add(new ExceptionCheck(conversionException, "Invalid NAL unit size"));
                        allExceptions.Add(new ExceptionCheck(conversionException, "Packet mismatch", true));

                        UnknownDecoderException unknownDecoderException = new UnknownDecoderException(exceptionOutput, args);
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "asf_read_pts failed", true));
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "Missing key frame while searching for timestamp", true));
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "Old interlaced mode is not supported", true));
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "mpeg1video", true));
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "Frame rate very high for a muxer not efficiently supporting it", true));
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "multiple fourcc not supported"));
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "Unknown decoder"));
                        allExceptions.Add(new ExceptionCheck(unknownDecoderException, "Failed to open codec in avformat_find_stream_info"));

                        HardwareAcceleratorNotFoundException hardwareAcceleratorNotFoundException = new HardwareAcceleratorNotFoundException(exceptionOutput, args);
                        allExceptions.Add(new ExceptionCheck(hardwareAcceleratorNotFoundException, "Unrecognized hwaccel: "));

                        foreach (ExceptionCheck item in allExceptions)
                        {
                            item.checkLog(_outputLog);
                        }

                        if (process.ExitCode != 0)
                        {
                            throw new ConversionException(exceptionOutput, args);
                        }
                    }
                }

                return true;
            },
                                         cancellationToken,
                                         TaskCreationOptions.LongRunning,
                                         TaskScheduler.Default));
        }
예제 #9
0
 public static RetrofitError conversionError(string url, string response, Converter.Converter converter,
                                             Type successType, ConversionException exception)
 {
     return(new RetrofitError(exception.Message, url, response, converter, successType,
                              Kind.CONVERSION, exception));
 }