Is there a way to flatten a Try? ``` Try...
# arrow
v
Is there a way to flatten a Try?
Copy code
Try {
            when (accessChecker.loggedInUserHasAccess()) {
                true -> Try.just(Unit)
                false -> Try.raise(FeatureAccessDeniedException("No access for feature"))
            }
        }
Or is better to just throw the error in the false branch. Note that
accessChecker.loggedInUser...
can also throw an exception.
Copy code
Try {
            when (accessChecker.loggedInUserHasAccess()) {
                true -> Unit
                false -> throw FeatureAccessDeniedException("No access for feature")
            }
        }
t
you could do:
Copy code
Try {
            when (accessChecker.loggedInUserHasAccess()) {
                true -> Try.just(Unit)
                false -> Try.raise(FeatureAccessDeniedException("No access for feature"))
            }
        }.flatMap { it }
👍 1
i.e. add
flatMap { it }
v
Good point! I think I'll just throw the exception within the
Try
block, it looks slightly cleaner.