Skip to content

Commit 552a212

Browse files
committed
Fix FileNotFoundException when storing segment values
1 parent 5272164 commit 552a212

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayCache.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class ReplayCache(
5555
internal val frames = mutableListOf<ReplayFrame>()
5656

5757
private val ongoingSegment = LinkedHashMap<String, String>()
58-
private val ongoingSegmentFile: File? by lazy {
58+
internal val ongoingSegmentFile: File? by lazy {
5959
if (replayCacheDir == null) {
6060
return@lazy null
6161
}
@@ -275,6 +275,9 @@ public class ReplayCache(
275275
if (isClosed.get()) {
276276
return
277277
}
278+
if (ongoingSegmentFile?.exists() != true) {
279+
ongoingSegmentFile?.createNewFile()
280+
}
278281
if (ongoingSegment.isEmpty()) {
279282
ongoingSegmentFile?.useLines { lines ->
280283
lines.associateTo(ongoingSegment) {

sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BaseCaptureStrategy.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.view.MotionEvent
55
import io.sentry.Breadcrumb
66
import io.sentry.DateUtils
77
import io.sentry.IScopes
8+
import io.sentry.SentryLevel.ERROR
89
import io.sentry.SentryOptions
910
import io.sentry.SentryReplayEvent.ReplayType
1011
import io.sentry.SentryReplayEvent.ReplayType.BUFFER
@@ -183,7 +184,11 @@ internal abstract class BaseCaptureStrategy(
183184
task()
184185
}
185186
} else {
186-
task()
187+
try {
188+
task()
189+
} catch (e: Throwable) {
190+
options.logger.log(ERROR, "Failed to execute task $TAG.runInBackground", e)
191+
}
187192
}
188193
}
189194

sentry-android-replay/src/test/java/io/sentry/android/replay/ReplayCacheTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,21 @@ class ReplayCacheTest {
285285
assertFalse(File(replayCache.replayCacheDir, ONGOING_SEGMENT).exists())
286286
}
287287

288+
@Test
289+
fun `when file does not exist upon persisting creates it`() {
290+
val replayId = SentryId()
291+
val replayCache = fixture.getSut(
292+
tmpDir,
293+
replayId
294+
)
295+
296+
replayCache.ongoingSegmentFile?.delete()
297+
298+
replayCache.persistSegmentValues("key", "value")
299+
val segmentValues = File(replayCache.replayCacheDir, ONGOING_SEGMENT).readLines()
300+
assertEquals("key=value", segmentValues[0])
301+
}
302+
288303
@Test
289304
fun `stores segment key value pairs`() {
290305
val replayId = SentryId()

0 commit comments

Comments
 (0)