Search code examples
javaexceptionplayframework-2.2recovery

Play Framework 2.2 (java) WS recovery block does not catch HTTP exception


(Seemingly) simple static method that invokes a known good web service.

Service returns HTTP 500 / Internal Server Error if a matching record is not found, but the recovery block of invokation is never executed.

Am I missing the obvious or doing something blatantly stupid?

public static Promise<Property> ByPhone(String phone) {
  return WS.url("http://localhost:9000/data/property/" + phone)
           .get ()
           .map (
              new Function<WS.Response, Property>() {
                public Property apply (WS.Response response) {
                  System.out.println("got here: " + response.getStatusText());
                  Property property = null;
                  try {
                    property = _mapper.readValue(response.getBody(), Property.class);
                  } catch (Throwable t) {
                    t.printStackTrace();
                  }
                  return property;
                }
              }
            ).recover (
              new Function<Throwable, Property>() {
                public Property apply (Throwable t) {
                  System.out.println("never get here");
                  t.printStackTrace();
                  return null;
                }
              }
            );
}

Solution

  • recover helps one recover from uncaught exceptions. In this case an HTTP 500 response doesn't result in an uncaught Throwable

    Presumably, 500 means that the following block will throw an error

    property = _mapper.readValue(response.getBody(), Property.class);
    

    However, you have this wrapped in a try catch block already.