Skip to content

Commit

Permalink
add tests for suppressed exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lbloder committed Apr 30, 2024
1 parent fe6dcf4 commit 7316651
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions sentry/src/test/java/io/sentry/SentryExceptionFactoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 7316651

Please sign in to comment.