public void Frames_Setter_ReplacesList()
        {
            var sut         = new SentryStackTrace();
            var original    = sut.Frames;
            var replacement = new List <SentryStackFrame>();

            sut.Frames = replacement;
            Assert.NotSame(original, replacement);
            Assert.Same(replacement, sut.Frames);
        }
Exemple #2
0
        public void Process_AttachStacktraceTrueAndExistentThreadInEvent_AddsNewThread()
        {
            var expected = new SentryStackTrace();

            _ = _fixture.SentryStackTraceFactory.Create(Arg.Any <Exception>()).Returns(expected);
            _fixture.SentryOptions.AttachStacktrace = true;
            var sut = _fixture.GetSut();

            Thread.CurrentThread.Name = "second";
            var evt = new SentryEvent {
                SentryThreads = new [] { new SentryThread {
                                             Name = "first"
                                         } }
            };

            _ = sut.Process(evt);

            Assert.Equal(2, evt.SentryThreads.Count());
            Assert.Equal("first", evt.SentryThreads.First().Name);
            Assert.Equal("second", evt.SentryThreads.Last().Name);
        }
        private static SentryException GetException(string condition, string stackTrace)
        {
            var frames  = new List <SentryStackFrame>();
            var exc     = condition.Split(new char[] { ':' }, 2);
            var excType = exc[0];
            // TODO: condition may NOT contain ':' separator
            var excValue  = exc.Length == 1 ? exc[0] : exc[1].Substring(1); // strip the space
            var stackList = stackTrace.Split('\n');

            // The format is as follows:
            // Module.Class.Method[.Invoke] (arguments) (at filename:lineno)
            // where :lineno is optional, will be ommitted in builds
            for (var i = 0; i < stackList.Length; i++)
            {
                string functionName;
                string filename;
                int    lineNo;

                var item = stackList[i];
                if (item == string.Empty)
                {
                    continue;
                }

                var closingParen = item.IndexOf(')');

                if (closingParen == -1)
                {
                    functionName = item;
                    lineNo       = -1;
                    filename     = string.Empty;
                }
                else
                {
                    try
                    {
                        functionName = item.Substring(0, closingParen + 1);
                        if (item.Substring(closingParen + 1, 5) != " (at ")
                        {
                            // we did something wrong, failed the check
                            Debug.Log("failed parsing " + item);
                            functionName = item;
                            lineNo       = -1;
                            filename     = string.Empty;
                        }
                        else
                        {
                            var colon = item.LastIndexOf(':', item.Length - 1, item.Length - closingParen);
                            if (closingParen == item.Length - 1)
                            {
                                filename = string.Empty;
                                lineNo   = -1;
                            }
                            else if (colon == -1)
                            {
                                filename = item.Substring(closingParen + 6, item.Length - closingParen - 7);
                                lineNo   = -1;
                            }
                            else
                            {
                                filename = item.Substring(closingParen + 6, colon - closingParen - 6);
                                lineNo   = Convert.ToInt32(item.Substring(colon + 1, item.Length - 2 - colon));
                            }
                        }
                    }
                    catch (Exception)
                    {
                        functionName = item;
                        lineNo       = -1;
                        filename     = string.Empty; // we have no clue
                    }
                }

                frames.Add(new SentryStackFrame
                {
                    FileName     = TryResolveFileNameForMono(filename),
                    AbsolutePath = filename,
                    Function     = functionName,
                    LineNumber   = lineNo,
                    InApp        = functionName != null &&
                                   !functionName.StartsWith("UnityEngine", StringComparison.Ordinal) &&
                                   !functionName.StartsWith("System", StringComparison.Ordinal)
                });
            }

            frames.Reverse();

            var stacktrace = new SentryStackTrace();

            foreach (var frame in frames)
            {
                stacktrace.Frames.Add(frame);
            }

            return(new SentryException
            {
                Stacktrace = stacktrace,
                Type = excType,
                Value = excValue
            });
        }
        public void Frames_Getter_NotNull()
        {
            var sut = new SentryStackTrace();

            Assert.NotNull(sut.Frames);
        }