WrappedResult
Overview
The WrappedResult
class is a sealed class used throughout the MineSec Headless SDK to encapsulate the result of operations. It provides a way to wrap successful results or exceptions, ensuring that exceptions are handled gracefully and do not crash the client's application.
Both successful and failed result will be treated as value (Error as value approach).
The pattern is an effective way to handle success and failure in a unified manner to distinguish success and failed operation, resulting in more maintainable and robust applications. It fits well within Kotlin's language features and ecosystem, making it a recommended approach for modern Kotlin applications.
Sub class
The WrappedResult
class has two subclasses: Success
and Failure
.
WrappedResult.Success
Represents a successful result.
Properties
value: T
: The successful result value.
Usage
val successResult = WrappedResult.Success(data)
WrappedResult.Failure
Represents a failed result. The contextual is the underlying error/ exception information.
This encapsulate the underlying error, like from CPoC/ MPoC SDK's MhdRuntimeException, network issues, and other exceptions.
Properties
code: Int
: The error code.message: String
: The error message.contextual: String?
: Optional contextual information about the error.extra: Map<String, String>?
: Optional additional information about the error.
Usage
val failureResult = WrappedResult.Failure(exception)
Examples
Handling Success and Failure
val result: WrappedResult<SomeData> = performOperation()
when (result) {
is WrappedResult.Success -> {
val data = result.value
// Handle success
}
is WrappedResult.Failure -> {
val errorCode = result.code
val errorMessage = result.message
// Handle failure
}
}