Handling error conditions is a core part of the software we write. However, we often treat it as a second class citizen, obscuring our intent through abuse of null values and exceptions that make our code hard to understand and maintain. In the functional programming community, it is common to use datatypes such as Option, Either or Validated to make our intentions explicit when dealing with errors. We can leverage the compiler to verify that we are handling them instead of hoping for the best at runtime. This results in code that is clearer, without hidden path flows. We’ll show how we have been doing this in Kotlin, with the help of the Arrow library.
49. /**
* Perform the verification against the given Token
*
* @param token to verify.
* @return a verified and decoded JWT.
* @throws AlgorithmMismatchException
* @throws SignatureVerificationException
* @throws TokenExpiredException
* @throws InvalidClaimException
*/
public DecodedJWT verifyByCallingExternalApi(String token);
59. sealed class Either<out L, out R> {
data class Left<out L, out R>(val a: L) : Either<L, R>()
data class Right<out L, out R>(val b: R) : Either<L, R>()
}