Skip to content

Commit

Permalink
Fix PSPDFKit error mapping (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga authored Jul 24, 2024
1 parent f43dbb3 commit dd68906
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import kotlin.reflect.KClass
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.readium.r2.shared.publication.ReadingProgression
import org.readium.r2.shared.util.ThrowableError
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.data.ReadError
import org.readium.r2.shared.util.data.ReadTry
Expand All @@ -43,30 +42,16 @@ public class PsPdfKitDocumentFactory(context: Context) : PdfDocumentFactory<PsPd
val innerDocument = PdfDocumentLoader.openDocument(context, documentSource)
Try.success(PsPdfKitDocument(innerDocument))
} catch (e: InvalidPasswordException) {
Try.failure(ReadError.Decoding(ThrowableError(e)))
Try.failure(ReadError.Decoding(e))
} catch (e: InvalidSignatureException) {
Try.failure(ReadError.Decoding(ThrowableError(e)))
Try.failure(ReadError.Decoding(e))
} catch (e: IOException) {
// For debugging purpose
dataProvider.error?.unwrapDebugException()
?.let { throw it }

dataProvider.error
?.let { Try.failure(it) }
?: throw IllegalStateException(e)
// Not a PDF or corrupted file.
?: Try.failure(ReadError.Decoding(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,7 +9,6 @@ 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.resource.Resource
Expand All @@ -30,17 +29,12 @@ internal class ResourceDataProvider(

private val length by lazy {
runBlocking {
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()
}
resource.length()
.getOrElse {
error = it
onResourceError(it)
DataProvider.FILE_SIZE_UNKNOWN.toLong()
}
}
}

Expand All @@ -57,17 +51,12 @@ internal class ResourceDataProvider(

override fun read(size: Long, offset: Long): ByteArray = runBlocking {
val range = offset until (offset + size)
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
}
resource.read(range)
.getOrElse {
error = it
onResourceError(it)
DataProvider.NO_DATA_AVAILABLE
}
}

override fun release() {
Expand Down

0 comments on commit dd68906

Please sign in to comment.