I created a (very) small library to make my life e...
# arrow
j
I created a (very) small library to make my life easier (and possible yours too 😉). I found that I was doing the same thing over and over again, which indicates that there is a better way to do it. In this case, I encountered it during HTTP endpoint implementations. In an HTTP endpoint, based on the request, you probably want to execute some meaningful logic. This means, using
arrow-fx-coroutines
, that it will be a suspended function. Personally I really like to work with `Either`s, so most often I end up with a signature like
suspend fun myMeaningfulLogic(): Either<DomainError, MeaningfulResult>
. When using such functions, you need to fold the returned
Either
so that you can create a response that is useful in an HTTP endpoint. And most often, just to be safe, because everything on the JVM can throw `Exception`s, you want to wrap your meaningful logic in an
Either.catch
. Which means more folding to come to a usable value. To simplify the
Either.catch
with multiple `Either.fold`s, I created a generic function that can handle any suspended function that returns an
Either
and return a desired type of value:
Copy code
get("/some/http/endpoint") { // This is an example using Ktor.  
    handle(
        logic = { myMeaningfulLogic() },
        ifSuccess = { a -> handleSuccess(call, a) },
        ifDomainError = { e -> handleDomainError(call, ::log, e) },
        ifSystemFailure = { throwable -> handleSystemFailure(call, ::log, throwable) },
        ifUnrecoverableState = ::log
    )
}
This generic handler is not limited to endpoint implementations. It can handle any message or event in any framework (there is also a handleBlocking variant) and possibly there are more use cases to use it. Feel free to check it out, see: https://github.com/sparetimedevs/pofpaf. Use it if you like it. Any feedback is welcome.
r
Thanks @Joram Visser this looks interesting to use Either at endpoints and other places when you don't want the potential Throwable to blow up. I wonder if it has minimal dependencies what it would look like as part of the Either API in Arrow even potentially as an Either.catch overload or similar.
j
I am open to such investigation and/or collaboration. The current implementation only depends on arrow. (Fun fact: the first iterations were using BIO and were more applied to one specific framework. And therefore also had dependencies on that framework. Over time, the function became more and more generic and the limitation and dependency to the framework was gone.)
r
We are definitely open to improve Either and If this fits in without dependencies it would definitely be useful. If you want to PR I'll be happy to review and guide with any Arrow nuances 🙏
j