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-failing future
  • i want handle separate cases of ws request succeeding vs failing. in both cases, want produce play response (likely future[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

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -