Skip to content

Commit

Permalink
Make possible crashes of PSPDFKit more explicit (readium#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga committed Apr 30, 2024
1 parent e42d495 commit 5a6a239
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,26 @@ public class PsPdfKitDocumentFactory(context: Context) : PdfDocumentFactory<PsPd
} catch (e: InvalidSignatureException) {
Try.failure(ReadError.Decoding(ThrowableError(e)))
} catch (e: IOException) {
Try.failure(
dataProvider.error
?: ReadError.UnsupportedOperation(ThrowableError(IllegalStateException(e)))
)
// For debugging purpose
dataProvider.error?.unwrapDebugException()
?.let { throw it }

dataProvider.error
?.let { Try.failure(it) }
?: throw IllegalStateException(e)
}
}

private fun ReadError.unwrapDebugException(): Throwable? {
if (this !is ReadError.UnsupportedOperation) {
return null
}

val throwableCause = (cause as? ThrowableError<*>)
?: return null

return throwableCause.throwable as? IllegalStateException
}
}

public class PsPdfKitDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ package org.readium.adapter.pspdfkit.document
import com.pspdfkit.document.providers.DataProvider
import java.util.UUID
import kotlinx.coroutines.runBlocking
import org.readium.r2.shared.util.ThrowableError
import org.readium.r2.shared.util.data.ReadError
import org.readium.r2.shared.util.getOrElse
import org.readium.r2.shared.util.isLazyInitialized
import org.readium.r2.shared.util.resource.Resource
import org.readium.r2.shared.util.resource.synchronized
import org.readium.r2.shared.util.toDebugDescription
Expand All @@ -30,12 +30,17 @@ internal class ResourceDataProvider(

private val length by lazy {
runBlocking {
resource.length()
.getOrElse {
error = it
onResourceError(it)
DataProvider.FILE_SIZE_UNKNOWN.toLong()
}
try {
resource.length()
.getOrElse {
error = it
onResourceError(it)
DataProvider.FILE_SIZE_UNKNOWN.toLong()
}
} catch (e: Exception) {
error = ReadError.UnsupportedOperation(ThrowableError(IllegalStateException(e)))
DataProvider.FILE_SIZE_UNKNOWN.toLong()
}
}
}

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

override fun read(size: Long, offset: Long): ByteArray = runBlocking {
val range = offset until (offset + size)
resource.read(range)
.getOrElse {
error = it
onResourceError(it)
DataProvider.NO_DATA_AVAILABLE
}
try {
resource.read(range)
.getOrElse {
error = it
onResourceError(it)
DataProvider.NO_DATA_AVAILABLE
}
} catch (e: Exception) {
error = ReadError.UnsupportedOperation(ThrowableError(IllegalStateException(e)))
DataProvider.NO_DATA_AVAILABLE
}
}

override fun release() {
if (::resource.isLazyInitialized) {
error = null
runBlocking { resource.close() }
}
runBlocking { resource.close() }
}
}

0 comments on commit 5a6a239

Please sign in to comment.