Skip to main content

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 successfully
  • Result.Failure - Contains a Throwable exception when an operation fails
Result class definition
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:

Common error types
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 errors
  • NullPointerException - Null reference encountered where valid reference was expected
  • IllegalArgumentException - Invalid argument passed to SDK function
  • IllegalStateException - 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:

Handle Result with when expression
loading...

Handling specific error types

You can check for specific error types to provide tailored error handling:

Handle specific error types
loading...

Using getter functions

Retrieve the result value directly using utility methods:

Handle Result with getters
loading...

Using chain API

For Kotlin users who prefer functional programming style, several chaining methods are available:

Handle Result with chain API
loading...

Using fold function

The fold function allows you to handle both success and failure cases in a single expression:

Handle Result with fold function
loading...

Chaining SDK operations

Results from multiple scanners can be chained together, with all exceptions along the chain handled appropriately:

Chaining multiple operations
loading...

Want to scan longer than one minute?

Generate a free trial license to test the Scanbot SDK thoroughly.

Get free trial license