Skip to content

Commit

Permalink
fix: robotoff should not make the app crash if unavailable (#4464)
Browse files Browse the repository at this point in the history
Closes #4357
  • Loading branch information
VaiTon committed Feb 5, 2022
1 parent 02c1c4e commit 70d4e09
Showing 1 changed file with 5 additions and 4 deletions.
Expand Up @@ -5,6 +5,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import okhttp3.Credentials
import openfoodfacts.github.scrachx.openfood.models.AnnotationAnswer
import openfoodfacts.github.scrachx.openfood.models.AnnotationResponse
import openfoodfacts.github.scrachx.openfood.models.Question
import openfoodfacts.github.scrachx.openfood.network.services.RobotoffAPI
import openfoodfacts.github.scrachx.openfood.utils.getLoginPreferences
import javax.inject.Inject
Expand All @@ -23,10 +24,10 @@ class RobotoffRepository @Inject constructor(
* @param lang is language of the question
* @return The single question
*/
suspend fun getProductQuestion(code: String, lang: String) =
robotoffAPI.getProductQuestions(code, lang, 1).questions
?.takeUnless { it.isEmpty() }
?.get(0)
suspend fun getProductQuestion(code: String, lang: String): Question? {
val state = runCatching { robotoffAPI.getProductQuestions(code, lang, 1) }
return state.getOrNull()?.questions?.getOrNull(0)
}

/**
* Annotate the Robotoff insight response using insight id and annotation
Expand Down

0 comments on commit 70d4e09

Please sign in to comment.