From 7316651b8d58c6663cd9d33a8e898605252d2544 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 30 Apr 2024 14:08:09 +0200 Subject: [PATCH] add tests for suppressed exceptions --- .../io/sentry/SentryExceptionFactoryTest.kt | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt index 888f17e0a3..84a51f1205 100644 --- a/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt @@ -209,6 +209,95 @@ class SentryExceptionFactoryTest { assertEquals(777, frame.lineno) } + @Test + fun `when exception has has suppressed exceptions, add them and show as group`() { + val exception = Exception("message") + val suppressedException = Exception("suppressed") + exception.addSuppressed(suppressedException) + + val mechanism = Mechanism() + mechanism.type = "ANR" + val thread = Thread() + val throwable = ExceptionMechanismException(mechanism, exception, thread) + + val queue = fixture.getSut().extractExceptionQueue(throwable) + + val suppressedInQueue = queue.pop() + val mainInQueue = queue.pop() + + assertEquals("suppressed", suppressedInQueue.value) + assertEquals(1, suppressedInQueue.mechanism?.exceptionId) + assertEquals(0, suppressedInQueue.mechanism?.parentId) + + assertEquals("message", mainInQueue.value) + assertEquals(0, mainInQueue.mechanism?.exceptionId) + assertEquals(true, mainInQueue.mechanism?.isExceptionGroup) + } + + @Test + fun `nested exception that contains suppressed exceptions is marked as group`() { + val exception = Exception("inner") + val suppressedException = Exception("suppressed") + exception.addSuppressed(suppressedException) + + val outerException = Exception("outer", exception) + + val queue = fixture.getSut().extractExceptionQueue(outerException) + + + val suppressedInQueue = queue.pop() + val mainInQueue = queue.pop() + val outerInQueue = queue.pop() + + assertEquals("suppressed", suppressedInQueue.value) + assertEquals(2, suppressedInQueue.mechanism?.exceptionId) + assertEquals(1, suppressedInQueue.mechanism?.parentId) + + assertEquals("inner", mainInQueue.value) + assertEquals(1, mainInQueue.mechanism?.exceptionId) + assertEquals(0, mainInQueue.mechanism?.parentId) + assertEquals(true, mainInQueue.mechanism?.isExceptionGroup) + + assertEquals("outer", outerInQueue.value) + assertEquals(0, outerInQueue.mechanism?.exceptionId) + assertNull(outerInQueue.mechanism?.parentId) + assertNull(outerInQueue.mechanism?.isExceptionGroup) + } + + @Test + fun `nested exception within Mechanism that contains suppressed exceptions is marked as group`() { + val exception = Exception("inner") + val suppressedException = Exception("suppressed") + exception.addSuppressed(suppressedException) + + val mechanism = Mechanism() + mechanism.type = "ANR" + val thread = Thread() + + val outerException = ExceptionMechanismException(mechanism, Exception("outer", exception), thread) + + val queue = fixture.getSut().extractExceptionQueue(outerException) + + + val suppressedInQueue = queue.pop() + val mainInQueue = queue.pop() + val outerInQueue = queue.pop() + + assertEquals("suppressed", suppressedInQueue.value) + assertEquals(2, suppressedInQueue.mechanism?.exceptionId) + assertEquals(1, suppressedInQueue.mechanism?.parentId) + + assertEquals("inner", mainInQueue.value) + assertEquals(1, mainInQueue.mechanism?.exceptionId) + assertEquals(0, mainInQueue.mechanism?.parentId) + assertEquals(true, mainInQueue.mechanism?.isExceptionGroup) + + assertEquals("outer", outerInQueue.value) + assertEquals(0, outerInQueue.mechanism?.exceptionId) + assertNull(outerInQueue.mechanism?.parentId) + assertNull(outerInQueue.mechanism?.isExceptionGroup) + } + internal class InnerClassThrowable constructor(cause: Throwable? = null) : Throwable(cause) private val anonymousException = object : Exception() {