Skip to content

Commit c7e12ae

Browse files
fix: Ensure that the Sentries we don't want are discarded (#2215)
2 parents 2662948 + 8d6dbe6 commit c7e12ae

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

app/src/main/java/com/infomaniak/mail/MainApplication.kt

+18-31
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import coil.ImageLoaderFactory
3636
import coil.decode.SvgDecoder
3737
import com.facebook.stetho.Stetho
3838
import com.infomaniak.lib.core.InfomaniakCore
39+
import com.infomaniak.lib.core.api.ApiController
3940
import com.infomaniak.lib.core.auth.TokenInterceptorListener
4041
import com.infomaniak.lib.core.models.user.User
4142
import com.infomaniak.lib.core.networking.AccessTokenUsageInterceptor
@@ -167,37 +168,23 @@ open class MainApplication : Application(), ImageLoaderFactory, DefaultLifecycle
167168
SentryAndroid.init(this) { options: SentryAndroidOptions ->
168169
// Register the callback as an option
169170
options.beforeSend = SentryOptions.BeforeSendCallback { event: SentryEvent, _: Any? ->
170-
171-
val shouldLog = mutableListOf<Boolean>()
172-
173-
// Sentry events are discarded if the app is in Debug mode
174-
val isInReleaseMode = !BuildConfig.DEBUG
175-
shouldLog.add(isInReleaseMode)
176-
177-
// Sentry events are discarded if the user deactivated Sentry tracking in DataManagement settings
178-
val isSentryTrackingEnabled = localSettings.isSentryTrackingEnabled
179-
shouldLog.add(isSentryTrackingEnabled)
180-
181-
// Network exceptions are discarded
182-
// TODO: It doesn't work anymore :(
183-
val isNetworkException = event.exceptions?.any { it.type == "ApiController\$NetworkException" } ?: false
184-
shouldLog.add(!isNetworkException)
185-
186-
// AccessDenied exceptions are discarded
187-
val isAccessDeniedException = event.exceptions?.any {
188-
// TODO: Check in Sentry if this `value.contains()` is the correct way to find this exception.
189-
it.type == "ApiErrorException" && it.value?.contains("access_denied") == true
190-
} ?: false
191-
shouldLog.add(!isAccessDeniedException)
192-
193-
// NotAuthorized exceptions are discarded
194-
val isNotAuthorizedException = event.exceptions?.any {
195-
// TODO: Check in Sentry if this `value.contains()` is the correct way to find this exception.
196-
it.type == "ApiErrorException" && it.value?.contains("not_authorized") == true
197-
} ?: false
198-
shouldLog.add(!isNotAuthorizedException)
199-
200-
if (shouldLog.all { true }) event else null
171+
val exception = event.throwable
172+
/**
173+
* Reasons to discard Sentry events :
174+
* - Application is in Debug mode
175+
* - User deactivated Sentry tracking in DataManagement settings
176+
* - The exception was an [ApiController.NetworkException], and we don't want to send them to Sentry
177+
* - The exception was an [ApiErrorException] with an [ErrorCode.ACCESS_DENIED] or
178+
* [ErrorCode.NOT_AUTHORIZED] error code, and we don't want to send them to Sentry
179+
*/
180+
when {
181+
BuildConfig.DEBUG -> null
182+
!localSettings.isSentryTrackingEnabled -> null
183+
exception is ApiController.NetworkException -> null
184+
exception is ApiErrorException && exception.errorCode == ErrorCode.ACCESS_DENIED -> null
185+
exception is ApiErrorException && exception.errorCode == ErrorCode.NOT_AUTHORIZED -> null
186+
else -> event
187+
}
201188
}
202189
options.addIntegration(
203190
FragmentLifecycleIntegration(

0 commit comments

Comments
 (0)