コード例 #1
0
        [Test] // ctor (int)
        public void Constructor1()
        {
            Win32Exception ex;

            ex = new Win32Exception(0);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#A1");
            Assert.IsNull(ex.InnerException, "#A2");
            Assert.IsNotNull(ex.Message, "#A3");
            Assert.IsFalse(ex.Message.IndexOf(ex.GetType().FullName) != -1, "#A4");
            Assert.AreEqual(0, ex.NativeErrorCode, "#A5");

            ex = new Win32Exception(int.MinValue);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#B1");
            Assert.IsNull(ex.InnerException, "#B2");
            Assert.IsNotNull(ex.Message, "#B3");
            Assert.IsFalse(ex.Message.IndexOf(ex.GetType().FullName) != -1, "#B4");
            Assert.AreEqual(int.MinValue, ex.NativeErrorCode, "#B5");

            ex = new Win32Exception(int.MaxValue);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#C1");
            Assert.IsNull(ex.InnerException, "#C2");
            Assert.IsNotNull(ex.Message, "#C3");
            Assert.IsFalse(ex.Message.IndexOf(ex.GetType().FullName) != -1, "#C4");
            Assert.AreEqual(int.MaxValue, ex.NativeErrorCode, "#C5");
        }
コード例 #2
0
        [Test] // ctor (string)
        public void Constructor2()
        {
            Win32Exception ex;
            string         msg          = "ERROR";
            int            native_error = Marshal.GetLastWin32Error();

            ex = new Win32Exception(msg);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#A1");
            Assert.IsNull(ex.InnerException, "#A2");
            Assert.AreSame(msg, ex.Message, "#A3");
            Assert.AreEqual(native_error, ex.NativeErrorCode, "#A4");

            ex = new Win32Exception((string)null);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#B1");
            Assert.IsNull(ex.InnerException, "#B2");
            Assert.IsNotNull(msg, ex.Message, "#B3");
            Assert.IsTrue(ex.Message.IndexOf(ex.GetType().FullName) != -1, "#B4");
            Assert.AreEqual(native_error, ex.NativeErrorCode, "#B5");

            ex = new Win32Exception(string.Empty);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#C1");
            Assert.IsNull(ex.InnerException, "#C2");
            Assert.IsNotNull(msg, ex.Message, "#C3");
            Assert.AreEqual(string.Empty, ex.Message, "#C4");
            Assert.AreEqual(native_error, ex.NativeErrorCode, "#C5");
        }
コード例 #3
0
        [Test] // ctor (int, string)
        public void Constructor3()
        {
            Win32Exception ex;
            string         msg = "ERROR";

            ex = new Win32Exception(int.MinValue, msg);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#A1");
            Assert.IsNull(ex.InnerException, "#A2");
            Assert.AreSame(msg, ex.Message, "#A3");
            Assert.AreEqual(int.MinValue, ex.NativeErrorCode, "#A4");

            ex = new Win32Exception(int.MaxValue, (string)null);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#B1");
            Assert.IsNull(ex.InnerException, "#B2");
            Assert.IsNotNull(msg, ex.Message, "#B3");
            Assert.IsTrue(ex.Message.IndexOf(ex.GetType().FullName) != -1, "#B4");
            Assert.AreEqual(int.MaxValue, ex.NativeErrorCode, "#B5");

            ex = new Win32Exception(0, msg);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#C1");
            Assert.IsNull(ex.InnerException, "#C2");
            Assert.AreSame(msg, ex.Message, "#C3");
            Assert.AreEqual(0, ex.NativeErrorCode, "#C4");

            ex = new Win32Exception(5, string.Empty);
            Assert.AreEqual(-2147467259, ex.ErrorCode, "#C1");
            Assert.IsNull(ex.InnerException, "#C2");
            Assert.IsNotNull(ex.Message, "#C3");
            Assert.AreEqual(string.Empty, ex.Message, "#C4");
            Assert.AreEqual(5, ex.NativeErrorCode, "#C5");
        }
コード例 #4
0
        [Test] // ctor ()
        public void Constructor0()
        {
            int native_error = Marshal.GetLastWin32Error();

            Win32Exception ex = new Win32Exception();

            Assert.AreEqual(-2147467259, ex.ErrorCode, "#1");
            Assert.IsNull(ex.InnerException, "#2");
            Assert.IsNotNull(ex.Message, "#3");
            Assert.IsFalse(ex.Message.IndexOf(ex.GetType().FullName) != -1, "#4");
            Assert.AreEqual(native_error, ex.NativeErrorCode, "#5");
        }
コード例 #5
0
        private static bool handleWin32Exception(Win32Exception ex, MethodBase Method)
        {
            Logging.Logger("ERROR: MethodName =" + Method.Name + "  Type: " + ex.GetType().Name + "  #:" + ex.NativeErrorCode + "  Message:" + ex.Message);
            switch (ex.NativeErrorCode)
            {
            case 1909:
                //Locked account
                PromptUser("ERROR:  MethodName=" + Method.Name + "  Type: " + ex.GetType().Name + "  #:" + ex.NativeErrorCode + "  Message:" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, "Network Error");
                SecurityTools.ClearAdminCreds();
                return(true);

            case 1326:
                //Bad credentials error. Clear AdminCreds
                PromptUser("ERROR:  MethodName=" + Method.Name + "  Type: " + ex.GetType().Name + "  #:" + ex.NativeErrorCode + "  Message:" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, "Network Error");
                SecurityTools.ClearAdminCreds();
                return(true);

            case 86:
                //Bad credentials error. Clear AdminCreds
                PromptUser("ERROR:  MethodName=" + Method.Name + "  Type: " + ex.GetType().Name + "  #:" + ex.NativeErrorCode + "  Message:" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, "Network Error");
                SecurityTools.ClearAdminCreds();
                return(true);

            case 5:
                //Access denied error. Clear AdminCreds
                PromptUser("ERROR:  MethodName=" + Method.Name + "  Type: " + ex.GetType().Name + "  #:" + ex.NativeErrorCode + "  Message:" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, "Network Error");
                SecurityTools.ClearAdminCreds();
                return(true);

            default:
                PromptUser("ERROR:  MethodName=" + Method.Name + "  Type: " + ex.GetType().Name + "  #:" + ex.NativeErrorCode + "  Message:" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, "Network Error");
                return(true);
            }
        }
コード例 #6
0
        public void ShouldSetInnerException()
        {
            var result   = Result.RzSuccess;
            var expected = new Win32Exception(result);
            var actual   = new NativeCallException("TestFunc", result).InnerException;

            Assert.AreEqual(expected.GetType(), actual.GetType(), "Expected types to be equal.");
            Assert.AreEqual(expected.HResult, actual.HResult, "Expected HResults to be equal.");
            Assert.AreEqual(expected.Message, actual.Message, "Expected message to be equal.");
            Assert.AreEqual(
                expected.NativeErrorCode,
                ((Win32Exception)actual).NativeErrorCode,
                "Expected native error codes to be equal.");
        }
コード例 #7
0
        private static string GetMessage(Win32Exception ex)
        {
            var errors = new StringBuilder();

            errors.AppendLine();
#if DEBUG
            errors.AppendLine($"[{ex.GetType().Name}]");
#endif

            switch (ex.ErrorCode)
            {
            //Maximum request length exceeded
            case 10060:
                errors.AppendLine("ارتباط با سرور، به دلیل عدم پاسخگویی سرور، وجود ندارد");
                break;

            default:
                errors.AppendLine($"{ex.ErrorCode}: {ex.Message}");
                break;
            }

            return(errors.ToString());
        }