Reputation: 1
I am new to scala. I am using the HTTP-client of scala to hit another server. Where the response returned is Future[HttpResponse]. Here is something that I have:
pathPrefix("run") {
post {
entity(as[InputRequest]) { inputRequest =>
complete {
runService(inputRequest)
}
}
}
}
def runService(inputRequest:InputRequest) : Future[HttpResponse] = {
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive ~> unmarshal[HttpResponse]
val response: Future[HttpResponse] = pipeline(Post("some-hostname", inputRequest)
~> addCredentials(BasicHttpCredentials("user", "pass"))
response
}
So if something fails(like 500 internal server error) from response, How to catch its exception?
Upvotes: 0
Views: 191
Reputation: 6718
Couple of ways to do it:
Using rejection handler directive:
handleRejections(/*your custom rejection handler || default*/) {
pathPrefix("run") {
...
}
Or recover
Future
:
response.recover {
case t => // t is Throwable, do something with it
}
The return type of your function def runService(inputRequest:InputRequest) : Future[HttpResponse]
will then change depending on what your recover evaluates to
Upvotes: 1