Skip to content

Commit 646297a

Browse files
Exclude CancellationException from sentry (#333)
1 parent bd7c646 commit 646297a

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

android/app-newm/src/main/java/io/newm/utils/AndroidNewmAppLogger.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import io.newm.shared.AppLogger
55
import io.newm.shared.public.analytics.NewmAppEventLogger
66
import io.sentry.Sentry
77
import io.sentry.protocol.User
8+
import kotlinx.coroutines.CancellationException
9+
import java.io.IOException
810

911
/**
1012
* An Android-specific implementation of [AppLogger] that logs messages
@@ -46,15 +48,23 @@ internal class AndroidNewmAppLogger(private val analyticsTracker: NewmAppEventLo
4648
}
4749

4850
/**
49-
* Logs an error message using Android's [Log] class and reports the exception to Sentry.
51+
* Logs an error using Android's [Log] and optionally reports the exception to Sentry.
52+
* Certain exceptions, like [CancellationException] and [IOException], are excluded from reporting.
5053
*
5154
* @param tag The tag identifying the source of the log message.
5255
* @param message The error message to log.
53-
* @param exception The exception associated with the error.
56+
* @param exception The exception to log and, if not excluded, report to Sentry.
5457
*/
5558
override fun error(tag: String, message: String, exception: Throwable) {
5659
Log.e(tag, message, exception)
57-
Sentry.captureException(exception)
60+
val exceptionsNotToReport = setOf(CancellationException::class, IOException::class)
61+
// Only send to Sentry if the exception is not in the blacklist
62+
if (exceptionsNotToReport.none { it.isInstance(exception) }) {
63+
Sentry.captureException(exception)
64+
} else {
65+
// Optionally log at a different level for known exceptions, like warnings
66+
Log.w(tag, "Excluded from Sentry reporting: ${exception::class.simpleName} - $message", exception)
67+
}
5868
}
5969

6070
/**

0 commit comments

Comments
 (0)