Skip to content

Commit dd68906

Browse files
authored
Fix PSPDFKit error mapping (#549)
1 parent f43dbb3 commit dd68906

File tree

2 files changed

+16
-42
lines changed

2 files changed

+16
-42
lines changed

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import kotlin.reflect.KClass
2121
import kotlinx.coroutines.Dispatchers
2222
import kotlinx.coroutines.withContext
2323
import org.readium.r2.shared.publication.ReadingProgression
24-
import org.readium.r2.shared.util.ThrowableError
2524
import org.readium.r2.shared.util.Try
2625
import org.readium.r2.shared.util.data.ReadError
2726
import org.readium.r2.shared.util.data.ReadTry
@@ -43,30 +42,16 @@ public class PsPdfKitDocumentFactory(context: Context) : PdfDocumentFactory<PsPd
4342
val innerDocument = PdfDocumentLoader.openDocument(context, documentSource)
4443
Try.success(PsPdfKitDocument(innerDocument))
4544
} catch (e: InvalidPasswordException) {
46-
Try.failure(ReadError.Decoding(ThrowableError(e)))
45+
Try.failure(ReadError.Decoding(e))
4746
} catch (e: InvalidSignatureException) {
48-
Try.failure(ReadError.Decoding(ThrowableError(e)))
47+
Try.failure(ReadError.Decoding(e))
4948
} catch (e: IOException) {
50-
// For debugging purpose
51-
dataProvider.error?.unwrapDebugException()
52-
?.let { throw it }
53-
5449
dataProvider.error
5550
?.let { Try.failure(it) }
56-
?: throw IllegalStateException(e)
51+
// Not a PDF or corrupted file.
52+
?: Try.failure(ReadError.Decoding(e))
5753
}
5854
}
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-
}
7055
}
7156

7257
public class PsPdfKitDocument(

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ 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
1312
import org.readium.r2.shared.util.data.ReadError
1413
import org.readium.r2.shared.util.getOrElse
1514
import org.readium.r2.shared.util.resource.Resource
@@ -30,17 +29,12 @@ internal class ResourceDataProvider(
3029

3130
private val length by lazy {
3231
runBlocking {
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-
}
32+
resource.length()
33+
.getOrElse {
34+
error = it
35+
onResourceError(it)
36+
DataProvider.FILE_SIZE_UNKNOWN.toLong()
37+
}
4438
}
4539
}
4640

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

5852
override fun read(size: Long, offset: Long): ByteArray = runBlocking {
5953
val range = offset until (offset + size)
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-
}
54+
resource.read(range)
55+
.getOrElse {
56+
error = it
57+
onResourceError(it)
58+
DataProvider.NO_DATA_AVAILABLE
59+
}
7160
}
7261

7362
override fun release() {

0 commit comments

Comments
 (0)