-
-
Notifications
You must be signed in to change notification settings - Fork 452
Add CoroutineExceptionHandler
#4259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9f3f129
Add CoroutineExceptionHandler
lcian ec61f30
tests
lcian 7f99901
Merge branch 'main' into lcian/feat/coroutine-exception-handler
lcian 122edef
Merge branch 'main' into lcian/feat/coroutine-exception-handler
lcian 14fa1b2
changelog
lcian 01011bf
improve
lcian 60134a0
improve
lcian 56040f5
Merge branch 'main' into lcian/feat/coroutine-exception-handler
lcian c1e5e0e
Merge branch 'main' into lcian/feat/coroutine-exception-handler
lcian 51c4f60
Update CHANGELOG.md
lcian 715d316
Merge branch 'main' into lcian/feat/coroutine-exception-handler
lcian a9ce800
Merge branch 'main' into lcian/feat/coroutine-exception-handler
lcian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryCoroutineExceptionHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.sentry.kotlin | ||
|
||
import io.sentry.IScopes | ||
import io.sentry.ScopesAdapter | ||
import io.sentry.SentryEvent | ||
import io.sentry.SentryLevel | ||
import io.sentry.exception.ExceptionMechanismException | ||
import io.sentry.protocol.Mechanism | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import org.jetbrains.annotations.ApiStatus | ||
import kotlin.coroutines.AbstractCoroutineContextElement | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* Captures exceptions thrown in coroutines (without rethrowing them) and reports them to Sentry as errors. | ||
*/ | ||
@ApiStatus.Experimental | ||
public open class SentryCoroutineExceptionHandler(private val scopes: IScopes = ScopesAdapter.getInstance()) : | ||
AbstractCoroutineContextElement(CoroutineExceptionHandler), CoroutineExceptionHandler { | ||
|
||
override fun handleException(context: CoroutineContext, exception: Throwable) { | ||
val mechanism = Mechanism().apply { | ||
type = "CoroutineExceptionHandler" | ||
} | ||
// the current thread is not necessarily the one that threw the exception | ||
lcian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val error = ExceptionMechanismException(mechanism, exception, Thread.currentThread()) | ||
val event = SentryEvent(error) | ||
lcian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
event.level = SentryLevel.ERROR | ||
scopes.captureEvent(event) | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...y-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryCoroutineExceptionHandlerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.sentry.kotlin | ||
|
||
import io.sentry.IScopes | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.runTest | ||
import org.mockito.kotlin.check | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
import kotlin.test.Test | ||
import kotlin.test.assertSame | ||
import kotlin.test.assertTrue | ||
|
||
class SentryCoroutineExceptionHandlerTest { | ||
|
||
class Fixture { | ||
val scopes = mock<IScopes>() | ||
|
||
fun getSut(): SentryCoroutineExceptionHandler { | ||
return SentryCoroutineExceptionHandler(scopes) | ||
} | ||
} | ||
|
||
@Test | ||
fun `captures unhandled exception in launch coroutine`() = runTest { | ||
val fixture = Fixture() | ||
val handler = fixture.getSut() | ||
val exception = RuntimeException("test") | ||
|
||
GlobalScope.launch(handler) { | ||
throw exception | ||
}.join() | ||
|
||
verify(fixture.scopes).captureEvent( | ||
check { | ||
assertSame(exception, it.throwable) | ||
} | ||
) | ||
} | ||
|
||
@Test | ||
fun `captures unhandled exception in launch coroutine with child`() = runTest { | ||
val fixture = Fixture() | ||
val handler = fixture.getSut() | ||
val exception = RuntimeException("test") | ||
|
||
GlobalScope.launch(handler) { | ||
launch { | ||
throw exception | ||
}.join() | ||
}.join() | ||
|
||
verify(fixture.scopes).captureEvent( | ||
check { | ||
assertSame(exception, it.throwable) | ||
} | ||
) | ||
} | ||
|
||
@Test | ||
fun `captures unhandled exception in async coroutine`() = runTest { | ||
val fixture = Fixture() | ||
val handler = fixture.getSut() | ||
val exception = RuntimeException("test") | ||
|
||
val deferred = GlobalScope.async() { | ||
throw exception | ||
} | ||
GlobalScope.launch(handler) { | ||
deferred.await() | ||
}.join() | ||
|
||
verify(fixture.scopes).captureEvent( | ||
check { | ||
assertTrue { exception.toString().equals(it.throwable.toString()) } // stack trace will differ | ||
} | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/CoroutinesUtil.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.sentry.samples.android | ||
|
||
import io.sentry.kotlin.SentryContext | ||
import io.sentry.kotlin.SentryCoroutineExceptionHandler | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import java.lang.RuntimeException | ||
|
||
object CoroutinesUtil { | ||
|
||
fun throwInCoroutine() { | ||
GlobalScope.launch(SentryContext() + SentryCoroutineExceptionHandler()) { | ||
throw RuntimeException("Exception in coroutine") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.