Skip to content

Commit c6b8fa5

Browse files
authored
fix: catch exception when parsing file for transcription (#42) (#406)
1 parent a27e22d commit c6b8fa5

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

core/src/main/java/org/openedx/core/module/TranscriptManager.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import okhttp3.OkHttpClient
55
import org.openedx.core.module.download.AbstractDownloader
66
import org.openedx.core.utils.Directories
77
import org.openedx.core.utils.IOUtils
8+
import org.openedx.core.utils.Logger
89
import org.openedx.core.utils.Sha1Util
910
import org.openedx.foundation.utils.FileUtil
1011
import subtitleFile.FormatSRT
@@ -21,6 +22,8 @@ class TranscriptManager(
2122
val fileUtil: FileUtil
2223
) {
2324

25+
private val logger = Logger(TAG)
26+
2427
private val transcriptDownloader = object : AbstractDownloader() {
2528
override val client: OkHttpClient
2629
get() = OkHttpClient.Builder().build()
@@ -62,17 +65,18 @@ class TranscriptManager(
6265
}
6366

6467
private suspend fun startTranscriptDownload(downloadLink: String) {
65-
if (!has(downloadLink)) {
66-
val file = File(getTranscriptDir(), Sha1Util.SHA1(downloadLink))
67-
val result = transcriptDownloader.download(
68-
downloadLink,
69-
file.path
70-
)
71-
if (result == AbstractDownloader.DownloadResult.SUCCESS) {
72-
getInputStream(downloadLink)?.let {
73-
val transcriptTimedTextObject =
74-
convertIntoTimedTextObject(it)
75-
transcriptObject = transcriptTimedTextObject
68+
if (has(downloadLink)) return
69+
val file = File(getTranscriptDir(), Sha1Util.SHA1(downloadLink))
70+
val result = transcriptDownloader.download(
71+
downloadLink,
72+
file.path
73+
)
74+
if (result == AbstractDownloader.DownloadResult.SUCCESS) {
75+
getInputStream(downloadLink)?.let {
76+
try {
77+
transcriptObject = convertIntoTimedTextObject(it)
78+
} catch (e: NullPointerException) {
79+
logger.e(throwable = e, submitCrashReport = true)
7680
}
7781
}
7882
}
@@ -86,7 +90,7 @@ class TranscriptManager(
8690
try {
8791
transcriptObject = convertIntoTimedTextObject(transcriptInputStream)
8892
} catch (e: Exception) {
89-
e.printStackTrace()
93+
logger.e(throwable = e, submitCrashReport = true)
9094
}
9195
} else {
9296
startTranscriptDownload(transcriptUrl)
@@ -127,6 +131,7 @@ class TranscriptManager(
127131
}
128132

129133
companion object {
134+
private const val TAG = "TranscriptManager"
130135
private const val FILE_VALIDITY_DURATION_HOURS = 5L
131136
}
132137
}

core/src/main/java/org/openedx/core/utils/Logger.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package org.openedx.core.utils
22

33
import android.util.Log
4+
import com.google.firebase.crashlytics.FirebaseCrashlytics
5+
import org.koin.core.component.KoinComponent
6+
import org.koin.core.component.inject
47
import org.openedx.core.BuildConfig
8+
import org.openedx.core.config.Config
9+
10+
class Logger(private val tag: String) : KoinComponent {
11+
12+
private val config by inject<Config>()
513

6-
class Logger(private val tag: String) {
714
fun d(message: () -> String) {
815
if (BuildConfig.DEBUG) Log.d(tag, message())
916
}
@@ -12,6 +19,13 @@ class Logger(private val tag: String) {
1219
if (BuildConfig.DEBUG) Log.e(tag, message())
1320
}
1421

22+
fun e(throwable: Throwable, submitCrashReport: Boolean = false) {
23+
if (BuildConfig.DEBUG) throwable.printStackTrace()
24+
if (submitCrashReport && config.getFirebaseConfig().enabled) {
25+
FirebaseCrashlytics.getInstance().recordException(throwable)
26+
}
27+
}
28+
1529
fun i(message: () -> String) {
1630
if (BuildConfig.DEBUG) Log.i(tag, message())
1731
}

0 commit comments

Comments
 (0)