web services - Converting a Future[T] to an always-successful Future[Option[T]] -
the motivating use-case have in mind follows:
- in play framework controller, calling play's
ws. produces potentially-failingfuture - i want handle separate cases of
wsrequest succeeding vs failing. in both cases, want produce playresponse(likelyfuture[response])
what think trying is:
- asynchronously, after future completes, unconditionally handle completion
- produce
future[response]corresponding desired response
i don't believe can use future.map because need customize handling of failure case , not pass on future's failure.
if have alternative suggestions how solve cleanly, please let me know.
you're looking future.recover.
val wsresponse: future[wsresponse] = ??? wsresponse map { response => // success case ok(response.json) } recover { // failure case, turn throwable response case t: throwable => internalservererror(t.getmessage) } recover takes partial function throwable => t. in case, t option[response] can construct according whatever business logic in both success , fail cases.
do note play's ws library return successful future failed http call, if (e.g.,) call returns 404 external server, map function still need error handling.
Comments
Post a Comment