Skip to content

Commit 5a6a239

Browse files
authored
Make possible crashes of PSPDFKit more explicit (readium#495)
1 parent e42d495 commit 5a6a239

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

readium/adapters/pspdfkit/document/src/main/java/org/readium/adapter/pspdfkit/document/PsPdfKitDocument.kt

+18-4
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,26 @@ public class PsPdfKitDocumentFactory(context: Context) : PdfDocumentFactory<PsPd
4747
} catch (e: InvalidSignatureException) {
4848
Try.failure(ReadError.Decoding(ThrowableError(e)))
4949
} catch (e: IOException) {
50-
Try.failure(
51-
dataProvider.error
52-
?: ReadError.UnsupportedOperation(ThrowableError(IllegalStateException(e)))
53-
)
50+
// For debugging purpose
51+
dataProvider.error?.unwrapDebugException()
52+
?.let { throw it }
53+
54+
dataProvider.error
55+
?.let { Try.failure(it) }
56+
?: throw IllegalStateException(e)
5457
}
5558
}
59+
60+
private fun ReadError.unwrapDebugException(): Throwable? {
61+
if (this !is ReadError.UnsupportedOperation) {
62+
return null
63+
}
64+
65+
val throwableCause = (cause as? ThrowableError<*>)
66+
?: return null
67+
68+
return throwableCause.throwable as? IllegalStateException
69+
}
5670
}
5771

5872
public class PsPdfKitDocument(

readium/adapters/pspdfkit/document/src/main/java/org/readium/adapter/pspdfkit/document/ResourceDataProvider.kt

+24-17
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ package org.readium.adapter.pspdfkit.document
99
import com.pspdfkit.document.providers.DataProvider
1010
import java.util.UUID
1111
import kotlinx.coroutines.runBlocking
12+
import org.readium.r2.shared.util.ThrowableError
1213
import org.readium.r2.shared.util.data.ReadError
1314
import org.readium.r2.shared.util.getOrElse
14-
import org.readium.r2.shared.util.isLazyInitialized
1515
import org.readium.r2.shared.util.resource.Resource
1616
import org.readium.r2.shared.util.resource.synchronized
1717
import org.readium.r2.shared.util.toDebugDescription
@@ -30,12 +30,17 @@ internal class ResourceDataProvider(
3030

3131
private val length by lazy {
3232
runBlocking {
33-
resource.length()
34-
.getOrElse {
35-
error = it
36-
onResourceError(it)
37-
DataProvider.FILE_SIZE_UNKNOWN.toLong()
38-
}
33+
try {
34+
resource.length()
35+
.getOrElse {
36+
error = it
37+
onResourceError(it)
38+
DataProvider.FILE_SIZE_UNKNOWN.toLong()
39+
}
40+
} catch (e: Exception) {
41+
error = ReadError.UnsupportedOperation(ThrowableError(IllegalStateException(e)))
42+
DataProvider.FILE_SIZE_UNKNOWN.toLong()
43+
}
3944
}
4045
}
4146

@@ -52,18 +57,20 @@ internal class ResourceDataProvider(
5257

5358
override fun read(size: Long, offset: Long): ByteArray = runBlocking {
5459
val range = offset until (offset + size)
55-
resource.read(range)
56-
.getOrElse {
57-
error = it
58-
onResourceError(it)
59-
DataProvider.NO_DATA_AVAILABLE
60-
}
60+
try {
61+
resource.read(range)
62+
.getOrElse {
63+
error = it
64+
onResourceError(it)
65+
DataProvider.NO_DATA_AVAILABLE
66+
}
67+
} catch (e: Exception) {
68+
error = ReadError.UnsupportedOperation(ThrowableError(IllegalStateException(e)))
69+
DataProvider.NO_DATA_AVAILABLE
70+
}
6171
}
6272

6373
override fun release() {
64-
if (::resource.isLazyInitialized) {
65-
error = null
66-
runBlocking { resource.close() }
67-
}
74+
runBlocking { resource.close() }
6875
}
6976
}

0 commit comments

Comments
 (0)