Esempio n. 1
0
        public void Extensions_Safe_Catch2()
        {
            var ex0    = new Ex("ex");
            var ex1    = new Ex1("ex");
            var ex2    = new Ex2("ex");
            var ex3    = new Ex3("ex");
            var baseEx = new BaseEx("ex");
            var subEx  = new SubEx("ex");

            var err1 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex1; });
            var err2 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex2; });

            Assert.IsFalse(err1.HasValue);
            Assert.IsFalse(err2.HasValue);

            Assert.IsInstanceOfType(err1.Match(x => null, ex => ex), typeof(Ex1));
            Assert.IsInstanceOfType(err2.Match(x => null, ex => ex), typeof(Ex2));

            Assert.AreEqual(err1.Match(x => null, ex => ex), ex1);
            Assert.AreEqual(err2.Match(x => null, ex => ex), ex2);

            CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex0; }));
            CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex1; }));

            Safe.Try <bool, BaseEx, Ex2>(() => { throw subEx; });
            Safe.Try <bool, Ex, Ex2>(() => { throw ex0; });
            Safe.Try <bool, Ex, Ex2>(() => { throw ex1; });

            var success = Safe.Try <bool, Ex1, Ex2>(() => true);

            Assert.IsTrue(success.ValueOr(false));
        }
Esempio n. 2
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Read a value from a TextReader.
        /// </summary>
        /// <remarks>
        /// If the wrapped parse method throws an exception, this method
        /// catches the exception, modifies its Data property, and then
        /// rethrows it.  A new key/value pair is set in the Data property:
        /// the key is "ParseMethod.Word", and the value is the word that
        /// was passed as a parameter to the parse method.
        /// </remarks>
        public InputValue <T> Read(StringReader reader,
                                   out int index)
        {
            //  Read word from reader.  A word is a sequence of 1 or more
            //  non-whitespace characters.
            TextReader.SkipWhitespace(reader);
            if (reader.Peek() == -1)
            {
                throw new InputValueException();
            }

            index = reader.Index;
            string word = TextReader.ReadWord(reader);

            try {
                return(new InputValue <T>(parseMethod(word), word));
            }
            catch (System.OverflowException) {
                string format = Type.GetNumericFormat <T>();
                if (format.Length > 0)
                {
                    format = string.Format("{{0:{0}}}", format);
                }
                else
                {
                    format = "{0}";
                }
                string min         = string.Format(format, Type.GetMinValue <T>());
                string max         = string.Format(format, Type.GetMaxValue <T>());
                string numericDesc = Type.GetNumericDescription <T>();
                numericDesc = String.PrependArticle(numericDesc);
                string message = string.Format("{0} is outside the range for {1}",
                                               word, numericDesc);
                MultiLineText range = new MultiLineText();
                range.Add(string.Format("Range is {0} to {1}", min, max));
                throw new InputValueException(word, message, range);
            }
            catch (System.Exception exc) {
                string message = string.Format("\"{0}\" is not a valid {1}",
                                               word,
                                               Type.GetDescription <T>());
                System.FormatException formatExc = exc as System.FormatException;
                //  Add the format message if it's not a system type (assume
                //  derived type is providing more detailed explanation).
                if (formatExc != null && !typeof(T).Namespace.StartsWith("System"))
                {
                    throw new InputValueException(word, message,
                                                  new MultiLineText(formatExc.Message));
                }
                else
                {
                    throw new InputValueException(word, message);
                }
            }
        }
Esempio n. 3
0
        public void Extensions_Safe_Catch5()
        {
            var ex0    = new Ex("ex");
            var ex1    = new Ex1("ex");
            var ex2    = new Ex2("ex");
            var ex3    = new Ex3("ex");
            var ex4    = new Ex4("ex");
            var ex5    = new Ex5("ex");
            var ex6    = new Ex6("ex");
            var baseEx = new BaseEx("ex");
            var subEx  = new SubEx("ex");

            var err1 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; });
            var err2 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex2; });
            var err3 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex3; });
            var err4 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex4; });
            var err5 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex5; });

            Assert.IsFalse(err1.HasValue);
            Assert.IsFalse(err2.HasValue);
            Assert.IsFalse(err3.HasValue);
            Assert.IsFalse(err4.HasValue);
            Assert.IsFalse(err5.HasValue);

            Assert.IsInstanceOf(typeof(Ex1), err1.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex2), err2.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex3), err3.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex4), err4.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex5), err5.Match(x => null, ex => ex));

            Assert.AreEqual(err1.Match(x => null, ex => ex), ex1);
            Assert.AreEqual(err2.Match(x => null, ex => ex), ex2);
            Assert.AreEqual(err3.Match(x => null, ex => ex), ex3);
            Assert.AreEqual(err4.Match(x => null, ex => ex), ex4);
            Assert.AreEqual(err5.Match(x => null, ex => ex), ex5);

            CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex0; }));
            CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex1; }));

            Safe.Try <bool, BaseEx, Ex2, Ex3, Ex4, Ex5>(() => { throw subEx; });
            Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex0; });
            Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; });

            var success = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => true);

            Assert.IsTrue(success.ValueOr(false));
        }
Esempio n. 4
0
 internal BookmarkFormatException(string bookmarkString, System.FormatException cause) : base(org.neo4j.kernel.api.exceptions.Status_Transaction.InvalidBookmark, string.Format("Supplied bookmark [{0}] does not conform to pattern {1}; unable to parse transaction id", bookmarkString, BOOKMARK_TX_PREFIX), cause)
 {
 }