diff --git a/app/src/main/java/org/mozilla/fenix/experiments/NimbusSetup.kt b/app/src/main/java/org/mozilla/fenix/experiments/NimbusSetup.kt index e2f14e671..2919ddadc 100644 --- a/app/src/main/java/org/mozilla/fenix/experiments/NimbusSetup.kt +++ b/app/src/main/java/org/mozilla/fenix/experiments/NimbusSetup.kt @@ -13,19 +13,22 @@ import mozilla.components.service.nimbus.NimbusAppInfo import mozilla.components.service.nimbus.NimbusDisabled import mozilla.components.service.nimbus.NimbusServerSettings import mozilla.components.support.base.log.logger.Logger +import org.mozilla.experiments.nimbus.internal.NimbusErrorException import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.R -import org.mozilla.fenix.components.isSentryEnabled import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.settings @Suppress("TooGenericExceptionCaught") fun createNimbus(context: Context, url: String?): NimbusApi { - val errorReporter: ((String, Throwable) -> Unit) = { message, e -> + val errorReporter: ((String, Throwable) -> Unit) = reporter@{ message, e -> Logger.error("Nimbus error: $message", e) - if (isSentryEnabled()) { - context.components.analytics.crashReporter.submitCaughtException(e) + + if (e is NimbusErrorException && !e.isReportableError()) { + return@reporter } + + context.components.analytics.crashReporter.submitCaughtException(e) } return try { // Eventually we'll want to use `NimbusDisabled` when we have no NIMBUS_ENDPOINT. @@ -96,3 +99,17 @@ fun createNimbus(context: Context, url: String?): NimbusApi { NimbusDisabled() } } + +/** + * Classifies which errors we should forward to our crash reporter or not. We want to filter out the + * non-reportable ones if we know there is no reasonable action that we can perform. + * + * This fix should be upstreamed as part of: https://github.com/mozilla/application-services/issues/4333 + */ +fun NimbusErrorException.isReportableError(): Boolean { + return when (this) { + is NimbusErrorException.RequestError, + is NimbusErrorException.ResponseError -> false + else -> true + } +} diff --git a/app/src/test/java/org/mozilla/fenix/experiments/NimbusSetupKtTest.kt b/app/src/test/java/org/mozilla/fenix/experiments/NimbusSetupKtTest.kt new file mode 100644 index 000000000..86dbf2ba6 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/experiments/NimbusSetupKtTest.kt @@ -0,0 +1,28 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.experiments + +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.mozilla.experiments.nimbus.internal.NimbusErrorException + +class NimbusSetupKtTest { + @Test + fun `WHEN error is reportable THEN return true`() { + val error = NimbusErrorException.IOError("bad error") + + assertTrue(error.isReportableError()) + } + + @Test + fun `WHEN error is non-reportable THEN return false`() { + val error1 = NimbusErrorException.ResponseError("oops") + val error2 = NimbusErrorException.RequestError("oops") + + assertFalse(error1.isReportableError()) + assertFalse(error2.isReportableError()) + } +}