Package com.rnett.krosstalk.result

APIs for KrosstalkResult. See the instructions on error handling.

Types

Link copied to clipboard
class KrosstalkHttpError(httpError: KrosstalkResult.HttpError) : KrosstalkException

An exception representing a non-success HTTP error code from a Krosstalk method.

Link copied to clipboard
interface KrosstalkResult<out T>

The result of a krosstalk call. Can be either an exception in the server method, a Http error, or success.

Link copied to clipboard
class KrosstalkServerException(exception: KrosstalkResult.ServerException) : KrosstalkException

An exception representing an exception on the server side (i.e. a HTTP 500 response, but with more information).

Link copied to clipboard

An exception representing an exception on the server side (i.e. a HTTP 500 response, but with more information).

Functions

Link copied to clipboard
inline fun <T> catchKrosstalkExceptions(block: () -> T): KrosstalkResult<T>

Catch and unwrap KrosstalkServerException and KrosstalkHttpError, without converting other exceptions to KrosstalkServerException.

Link copied to clipboard
inline fun <T, R> KrosstalkResult<T>.fold(onSuccess: (KrosstalkResult.Success<T>) -> R, onFailure: (KrosstalkResult.Failure) -> R): R
inline fun <T, R> KrosstalkResult<T>.fold(onSuccess: (KrosstalkResult.Success<T>) -> R, onHttpError: (KrosstalkResult.HttpError) -> R, onServerException: (KrosstalkResult.ServerException) -> R): R

Get a value depending on the type of result.

Link copied to clipboard
inline fun <T, R> KrosstalkResult.SuccessOrHttpError<T>.foldHttpError(onSuccess: (KrosstalkResult.Success<T>) -> R, onHttpError: (KrosstalkResult.HttpError) -> R): R

Get a value depending on the type of result.

Link copied to clipboard
inline fun <T, R> KrosstalkResult.SuccessOrServerException<T>.foldServerException(onSuccess: (KrosstalkResult.Success<T>) -> R, onServerException: (KrosstalkResult.ServerException) -> R): R

Get a value depending on the type of result.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult<T>.getOrDefault(onFailure: R): R

Get the value on success, or onFailure otherwise.

inline fun <R, T : R> KrosstalkResult<T>.getOrDefault(onServerException: R, onHttpError: R): R

Get the value on success, onServerException on a server exception, or onHttpError on a http error.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult<T>.getOrElse(onFailure: (KrosstalkResult.Failure) -> R): R

Gets the value on success, or the result of onFailure otherwise.

inline fun <R, T : R> KrosstalkResult<T>.getOrElse(onServerException: (KrosstalkResult.ServerException) -> R, onHttpError: (KrosstalkResult.HttpError) -> R): R

Get the value on success, onServerException on a server exception, or onHttpError on a http error.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult.SuccessOrHttpError<T>.getOrElseHttpError(onHttpError: (KrosstalkResult.HttpError) -> R): R

Gets the value on success, or the result of onFailure otherwise.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult.SuccessOrServerException<T>.getOrElseServerException(onServerException: (KrosstalkResult.ServerException) -> R): R

Gets the value on success, or the result of onFailure otherwise.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult<T>.handleHttpError(filter: (KrosstalkResult.HttpError) -> Boolean, onHttpError: (KrosstalkResult.HttpError) -> R): KrosstalkResult<R>

Handle http errors matching filter.

inline fun <R, T : R> KrosstalkResult.SuccessOrHttpError<T>.handleHttpError(statusCode: Int, onHttpError: (KrosstalkResult.HttpError) -> R): KrosstalkResult.SuccessOrHttpError<R>
inline fun <R, T : R> KrosstalkResult<T>.handleHttpError(statusCode: Int, onHttpError: R): KrosstalkResult<R>
inline fun <R, T : R> KrosstalkResult<T>.handleHttpError(statusCode: Int, onHttpError: (KrosstalkResult.HttpError) -> R): KrosstalkResult<R>

Handle http errors with status codes of statusCode.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult<T>.handleServerException(filter: (KrosstalkResult.ServerException) -> Boolean, onServerException: (KrosstalkResult.ServerException) -> R): KrosstalkResult<R>

Handle server exceptions matching filter.

Handle server exceptions with a KrosstalkResult.ServerException.className of className.

inline fun <R, T : R> KrosstalkResult<T>.handleServerException(className: String, onServerException: (KrosstalkResult.ServerException) -> R): KrosstalkResult<R>

Handle server exceptions with a KrossalkResult.ServerException.className of className.

Link copied to clipboard

Handle server exceptions matching filter as http errors.

inline fun <T> KrosstalkResult<T>.handleServerExceptionAsHttpError(filter: (KrosstalkResult.ServerException) -> Boolean, statusCode: Int): KrosstalkResult<T>

Handle server exceptions matching filter as http errors with status code statusCode and a message of KrossalkResult.ServerException.asString.

inline fun <T> KrosstalkResult<T>.handleServerExceptionAsHttpError(className: String, onServerException: (KrosstalkResult.ServerException) -> KrosstalkResult.HttpError): KrosstalkResult<T>

Handle server exceptions with class names of className as http errors.

inline fun <T> KrosstalkResult<T>.handleServerExceptionAsHttpError(className: String, statusCode: Int): KrosstalkResult<T>

Handle server exceptions with class names of className as http errors with status code statusCode and a message of KrossalkResult.ServerException.asString.

Link copied to clipboard
inline fun <T> KrosstalkResult<T>.isFailure(): Boolean

Whether the result is not a success.

Link copied to clipboard
inline fun <T> KrosstalkResult<T>.isHttpError(): Boolean

Whether the result is a http error.

Link copied to clipboard
inline fun <T> KrosstalkResult<T>.isServerException(): Boolean

Whether the result is a server exception.

Link copied to clipboard
inline fun <T> KrosstalkResult<T>.isSuccess(): Boolean

Whether the result is success.

Link copied to clipboard
inline fun <T, R> KrosstalkResult.SuccessOrHttpError<T>.map(transform: (T) -> R): KrosstalkResult.SuccessOrHttpError<R>
inline fun <T, R> KrosstalkResult.SuccessOrServerException<T>.map(transform: (T) -> R): KrosstalkResult.SuccessOrServerException<R>
inline fun <T, R> KrosstalkResult<T>.map(transform: (T) -> R): KrosstalkResult<R>

Transform the success value.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult.SuccessOrHttpError<T>.recoverHttpErrors(onHttpError: (KrosstalkResult.HttpError) -> R): R

Recover from all http errors. Note that exceptions will not be caught, so KrosstalkResult.HttpError.throwFailureException can be used to throw on unhandled http errors.

inline fun <R, T : R> KrosstalkResult<T>.recoverHttpErrors(onHttpError: (KrosstalkResult.HttpError) -> R): KrosstalkResult.SuccessOrServerException<R>

Recover from all http errors. Note that exceptions will not be caught, so KrossalkResult.HttpError.throwFailureException can be used to throw on unhandled http errors.

Link copied to clipboard
inline fun <R, T : R> KrosstalkResult.SuccessOrServerException<T>.recoverServerExceptions(onServerException: (KrosstalkResult.ServerException) -> R): R

Recover from all server exceptions. Note that exceptions will not be caught, so KrosstalkResult.ServerException.throwFailureException can be used to throw on unhandled server exceptions.

inline fun <R, T : R> KrosstalkResult<T>.recoverServerExceptions(onServerException: (KrosstalkResult.ServerException) -> R): KrosstalkResult.SuccessOrHttpError<R>

Recover from all server exceptions. Note that exceptions will not be caught, so KrossalkResult.ServerException.throwFailureException can be used to throw on unhandled server exceptions.

Link copied to clipboard

Recover from all server exceptions, converting them to a successful value or a http error.

Link copied to clipboard
inline fun <T> runKrosstalkCatching(includeStackTrace: Boolean = true, block: () -> T): KrosstalkResult<T>

Runs block and wraps the result if it is a success. If block throws, it wraps the resulting exception in KrosstalkResult.ServerException.

Link copied to clipboard
inline fun throwKrosstalkHttpError(statusCode: Int, message: String? = null): Nothing
Link copied to clipboard
inline fun throwKrosstalkServerException(throwable: Throwable, includeStackTrace: Boolean = true): Nothing
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
inline fun <T> Result<T>.toKrosstalkResult(includeStackTrace: Boolean = true): KrosstalkResult<T>

Convert a Result to a KrosstalkResult, using KrosstalkResult.ServerException to represent a failure.

Link copied to clipboard
inline fun <T> KrosstalkResult<T>.toResult(): Result<T>

Convert to a Result, using the KrosstalkHttpError or KrosstalkServerException exceptions.

Properties

Link copied to clipboard
val <T> KrosstalkResult<T>.failureOrNull: KrosstalkResult.Failure?

Get the failure if there is one, else null

Link copied to clipboard
val <T> KrosstalkResult<T>.httpErrorOrNull: KrosstalkResult.HttpError?

Get the http error if there is one, else null

Link copied to clipboard

Get the server exception if there is one, else null

Link copied to clipboard
val <T> KrosstalkResult<T>.valueOrNull: T?

Get the value if successful, else null

Link copied to clipboard
val <T> KrosstalkResult<T>.valueOrThrow: T

Get the value if successful, otherwise throw with throwOnFailure