Example #1
0
        /// <summary>
        /// Converts a exception to a proto exception
        /// </summary>
        /// <param name="p_Exception">The p_Exception<see cref="S_Exception"/></param>
        /// <param name="throwException">The throwException<see cref="bool"/></param>
        /// <returns>the proto Exception</returns>
        public static Exception ToException(this Any proto, bool throwException = false)
        {
            Exception   ex          = null;
            S_Exception p_Exception = proto.CastToModel <S_Exception>();

            if (p_Exception == null)
            {
                throw new Exception("Excpected S_Exception proto, but received: " + p_Exception.GetType().FullName);
            }

            if (p_Exception != null)
            {
                ex = p_Exception.ToBaseException();
            }


            if (!string.IsNullOrEmpty(p_Exception.Message) && ex != null)
            {
                if (throwException)
                {
                    throw new Exception(p_Exception.Message, ex);
                }
                return(new Exception(p_Exception.Message, ex));
            }
            else if (!string.IsNullOrEmpty(p_Exception.Message))
            {
                if (throwException)
                {
                    throw new Exception(p_Exception.Message);
                }
                return(new Exception(p_Exception.Message));
            }

            if (throwException)
            {
                throw ex;
            }

            return(ex);
        }