Result API in the Kotlin Multiplatform Document SDK
The Scanbot SDK for Kotlin Multiplatform uses a generic Result<T> class that provides a unified approach to error handling across all platforms. This class is returned by most API methods, including scanner creation and execution operations.
Result class
The Result<T> class is a sealed class with two possible states:
Result.Success<T>- Contains the expected result value when an operation completes successfullyResult.Failure- Contains aThrowableexception when an operation fails
sealed class Result<out T> {
data class Success<T>(val value: T) : Result<T>()
data class Failure(val exception: Throwable) : Result<Nothing>()
}
On successful execution, the API returns Result.Success, which contains the expected result object. If an error occurs—such as a license issue or an internal exception—the API returns Result.Failure containing the exception details.
You can find the Result API references here.
Error types
When operations fail, the Result.Failure contains a Throwable exception. The KMP SDK defines the following common error types that are shared across all platforms:
class InvalidLicenseError(message: String) : Throwable(message)
class InvalidImageRefError(message: String) : Throwable(message)
class ComponentUnavailableError(message: String) : Throwable(message)
class IoError(message: String) : Throwable(message)
class InvalidDataError(message: String) : Throwable(message)
class OperationCanceledError(message: String) : Throwable(message)
class TimeoutError(message: String) : Throwable(message)
class OutOfMemoryError(message: String) : Throwable(message)
In addition to these common errors, the SDK can also throw platform-standard exceptions:
RuntimeException- Unknown or unexpected errorsNullPointerException- Null reference encountered where valid reference was expectedIllegalArgumentException- Invalid argument passed to SDK functionIllegalStateException- SDK is in an inconsistent state for the requested operation
Handling results
The Result class provides multiple ways to handle operation outcomes, allowing you to choose the approach that best fits your project needs.
Using when expression
Handle results using Kotlin's when expression for explicit success and failure handling:
loading...
Handling specific error types
You can check for specific error types to provide tailored error handling:
loading...
Using getter functions
Retrieve the result value directly using utility methods:
loading...
Using chain API
For Kotlin users who prefer functional programming style, several chaining methods are available:
loading...
Using fold function
The fold function allows you to handle both success and failure cases in a single expression:
loading...
Chaining SDK operations
Results from multiple scanners can be chained together, with all exceptions along the chain handled appropriately:
loading...
Want to scan longer than one minute?
Generate a free trial license to test the Scanbot SDK thoroughly.
Get free trial licenseScanbot SDK is part of the Apryse SDK product family
A mobile scan is just the start. With Apryse SDKs, you can expand mobile workflows into full cross‑platform document processing. Whether you need to edit PDFs, add secure digital signatures, or use a fast, customizable document viewer and editor, Apryse gives you the tools to build powerful features quickly.
Learn more
