I came across functions that return Future , but also immediately throw exceptions. For example, for example:
def func(): Future[String] { if (something) { // this Future.failed(new RuntimeException("test")) } else { // and this throw new RuntimeException("test") } }
This behavior seems annoying to the caller because you have to do something similar to catch both errors:
try { func() recover { case e: Exception => handleError(e) } } catch { case e: Exception => Future.successful(handleError(e))
I noticed that the WSClient in play structure does this (both throw exceptions if the URL is incorrect, and returns Future , which fails if the HTTP request fails).
Is this a good practice? Is there a better way to handle errors from functions that behave this way?
scala
Tom haigh
source share