예제 #1
0
        public static void LogException(this Exception ex)
        {
            // Get the stack trace from the given exception
            StackTrace st      = new StackTrace(ex);
            int        nOffset = -1;

            // check if it contains valid info
            if (st.FrameCount == 0)
            {
                // nothing found, so we create new Stack Trace
                st      = new StackTrace();
                nOffset = 2;
            }
            else
            {
                nOffset = 0;
            }
            // only if we have valid offset
            CodeException codeEx = new CodeException();

            codeEx.MachineName   = Environment.MachineName;
            codeEx.ExceptionTime = DateTime.Now;
            //codeEx.UserId = System.Threading.Thread.CurrentPrincipal.Identity.Name;
            if (nOffset >= 0)
            {
                // get the current frame
                StackFrame sf = st.GetFrame(nOffset);
                codeEx.MethodName      = sf.GetMethod().Name;
                codeEx.ClassName       = sf.GetMethod().DeclaringType.Name;
                codeEx.AssemblyName    = sf.GetMethod().DeclaringType.Assembly.FullName;;
                codeEx.AssemblyVersion = sf.GetMethod().DeclaringType.Assembly.ImageRuntimeVersion;
                codeEx.StackDump       = st.ToString();
            }

            codeEx.ErrorMessage = ex.Message;

            if (ex is DataException)
            {
                DataException x = ex as DataException;
                if (x.Data != null)
                {
                    foreach (var key in x.Data.Keys)
                    {
                        codeEx.StackDump += key + ":" + x.Data[key] + Environment.NewLine;
                    }
                }
            }

            while (ex.InnerException != null)
            {
                codeEx.ErrorMessage += Environment.NewLine + ex.InnerException.Message;
                ex = ex.InnerException;
            }

            using (var dbContext = new VSDBContext())
            {
                dbContext.CodeExceptions.Add(codeEx);
                dbContext.SaveChanges();
            }
        }
예제 #2
0
        public void ShouldGetCodeExceptionWhenNoArgumentAndReturnCodeException()
        {
            var actual   = _allErrors.NoArgumentReturnCodeException();
            var expected = new CodeException("100003", "no argument return code exception.");

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #3
0
        public void ShouldGetCodeExceptionWhenMissingIndexArgument()
        {
            var actual   = _allErrors.MissingIndexArgument();
            var expected = new CodeException("100013", "value is [null].");

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #4
0
        public void ShouldGetConfigValueWhenConfigSomeValueInI18NResx()
        {
            var actual   = _allErrors.ConfigSomeValueInI18NResx();
            var expected = new CodeException("200003", "abc");

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #5
0
        public void ShouldGetEmptyMessageWhenConfigEmptyTemplateInI18NResx()
        {
            var actual   = _allErrors.ConfigEmptyTemplateInI18NResx();
            var expected = new CodeException("200002", string.Empty);

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #6
0
        public void ShouldGetCodeTemplateMessageWhenNoConfigKeyInI18NResx()
        {
            var actual   = _allErrors.NotConfigKeyInI18NResx();
            var expected = new CodeException("200001", "code template first");

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #7
0
				public DebugErrorMarker(TextMarkerService svc, CodeException ex)
					: base(svc, svc.Editor.Document.GetOffset((int)ex.SourceLine, 0), true)
				{
					this.Exception = ex;
					ToolTip = ex.Message;

					BackgroundColor = Colors.Yellow;
				}
예제 #8
0
                public DebugErrorMarker(TextMarkerService svc, CodeException ex)
                    : base(svc, svc.Editor.Document.GetOffset((int)ex.SourceLine, 0), true)
                {
                    this.Exception = ex;
                    ToolTip        = ex.Message;

                    BackgroundColor = Colors.Yellow;
                }
예제 #9
0
        public void ShouldGetFormatMessageWhenConfigIndexTemplateValueInI18NResx()
        {
            var actual   = _allErrors.ConfigIndexTemplateValueInI18NResx(1);
            var expected = new CodeException("200005", "value is   001.")
                           .WithData("val", 1);

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #10
0
        public void ShouldGetCodeExceptionWhenWithIndexArgument()
        {
            var actual   = _allErrors.WithIndexArgument("abc");
            var expected = new CodeException(100006, "the argument value is abc.");

            expected.Data["arg"] = "abc";
            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #11
0
        public void ShouldGetCodeExceptionWhenWithNameArgumentAndHasFormat()
        {
            var actual   = _allErrors.WithNameArgumentAndHasFormat(12);
            var expected = new CodeException("100008", "the argument value is 012.");

            expected.Data["arg"] = 12;
            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #12
0
        public void ShouldGetCodeExceptionWithEmptyStringMessageWhenMessageTemplateIsNull()
        {
            var actual   = _allErrors.NullTemplate("abc");
            var expected = new CodeException("100016", string.Empty)
                           .WithData("arg", "abc");

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #13
0
        public void ShouldGetCodeExceptionWhenWithNameArgumentAndHasDefaultValue()
        {
            var actual   = _allErrors.WithNameArgumentAndHasDefaultValue();
            var expected = new CodeException("100007", "the argument value is abc.");

            expected.Data["arg"] = "abc";
            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #14
0
        public void ShouldGetCodeExceptionWithExceptionWhenUseIndexArgument()
        {
            var actual   = _allErrors.WithInnerExceptionAndHasIndexArgument(new Exception("some error"), "abc");
            var expected = new CodeException("100015", "Value 'abc' Error 'System.Exception: some error'.")
                           .WithException(new Exception("some error"))
                           .WithData("arg", "abc");

            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #15
0
        public void ShouldGetCodeExceptionWhenMixedNameArgumentAndIndexArgument()
        {
            var actual   = _allErrors.MixedNameArgumentAndIndexArgument(12, 13);
            var expected = new CodeException("100011", "first value is 012, second value is 013.");

            expected.Data["arg1"] = 12;
            expected.Data["arg2"] = 13;
            actual.Should().BeOfType <CodeException>()
            .Which.Should().BeEquivalentTo(expected);
        }
예제 #16
0
        private Exception BuildException(int code, string message, IDictionary <string, object> data,
                                         Exception innerException)
        {
            var exception = new CodeException(code, message, innerException);

            // data ignore inner exception
            foreach (var(key, value) in data)
            {
                if (key != null && value != innerException)
                {
                    exception.Data[key] = value;
                }
            }
            return(exception);
        }
예제 #17
0
        public void Assert()
        {
            if (Code != default)
            {
                var exception = new CodeException(Code, Message);
                if (ErrorData != null)
                {
                    foreach (var kv in ErrorData)
                    {
                        exception.Data[kv.Key] = kv.Value;
                    }
                }

                throw exception;
            }
        }
예제 #18
0
 public LoginException(string message, CodeException code = CodeException.Default) : base(message)
 {
     Code = code;
 }
예제 #19
0
        public static ExceptionModel GetExceptionModel(this Exception exception)
        {
            ApiException apiException = exception as ApiException;

            if (apiException != null)
            {
                if (apiException.StatusCode == StatusCodes.Status401Unauthorized)
                {
                    return(new ExceptionModel()
                    {
                        Code = apiException.StatusCode,
                        Message = "Unauthorized",
                        Severity = Severity.Error,
                        Source = apiException.Source
                    });
                }

                List <ExceptionModel> innerExceptions = new List <ExceptionModel>();

                foreach (BusinessException ex in apiException.BusinessExceptions)
                {
                    innerExceptions.Add(new ExceptionModel()
                    {
                        Code     = ex.Code,
                        Message  = ex.Message,
                        Severity = ex.Severity,
                        Source   = ex.Source
                    });
                }

                ExceptionModel exceptionModel = new ExceptionModel()
                {
                    Code     = apiException.StatusCode,
                    Message  = apiException.Message,
                    Severity = Severity.Error,
                    Source   = apiException.Source
                };

                if (innerExceptions != null && innerExceptions.Count > 0)
                {
                    exceptionModel.InnerExceptionModels = innerExceptions;
                }

                return(exceptionModel);
            }

            BusinessException businessException = exception as BusinessException;

            if (businessException != null)
            {
                return(new ExceptionModel()
                {
                    Code = businessException.Code,
                    Message = businessException.Message,
                    Severity = businessException.Severity,
                    Source = businessException.Source
                });
            }

            CodeException codeException = exception as CodeException;

            if (codeException != null)
            {
                return(new ExceptionModel()
                {
                    Code = codeException.Code,
                    Message = codeException.Message,
                    Severity = Severity.Error,
                    Source = codeException.Source
                });
            }

            return(new ExceptionModel()
            {
                Code = StatusCodes.Status500InternalServerError,
                Message = "Internal server error occurred.",
                Severity = Severity.Error,
                Source = exception?.Source
            });
        }