Skip to content

Make possible crashes of PSPDFKit more explicit #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() }
}
}
Loading