When performing http requests, it is difficult to obtain the response body and headers if the status is not 2xx. The exceptions thrown if the response is not 2xx only contain a message String. The only way to obtain the response body is to possibly perform some regex on the message to extract it out. What would be nicer if a user was able to write something like this:
http.POST(url, body).map{ response: HttpResponse =>
response.status match {
case 200 => /** do something */
case 401 => /** do something else */
...
}
}
This is impossible however as exception are being thrown before we get the map in the example above. Doing this:
http.POST(url, body).recover{
case e: BadRequestException => /** do something */
}
is problematic as the exception only contains a message String. There is no easy way of using the body of the response during error handling.
Could the code be refactored so it doesn't throw exceptions if it's returning a HttpResponse or can the exceptions be made to include more easily accessible information?
When performing http requests, it is difficult to obtain the response body and headers if the status is not
2xx. The exceptions thrown if the response is not2xxonly contain a messageString. The only way to obtain the response body is to possibly perform some regex on the message to extract it out. What would be nicer if a user was able to write something like this:This is impossible however as exception are being thrown before we get the
mapin the example above. Doing this:is problematic as the exception only contains a message
String. There is no easy way of using the body of the response during error handling.Could the code be refactored so it doesn't throw exceptions if it's returning a
HttpResponseor can the exceptions be made to include more easily accessible information?