Search code examples
javafacebookfacebook-graph-apijerseyjersey-client

How to parse HTTP 400 response using Jersey Client into a Java Class?


I am using Jersey client to interact with the Facebook Graph API. The Jersey client helps me parse JSON responses into Java classes.

Sometimes Facebook sends a 400 response along with useful information about the reason for the 400 response. For example: {"error":{"message":"Error validating application. Cannot get application info due to a system error.","type":"OAuthException","code":101}}

Jersey simply throws an exception and eats up the useful part of the response :-(

How do I get it to parse the JSON into a Java class with fields corresponding to the useful error information?


Solution

  • this code works:

    try {
      User user = user_target
          .request()
          .get(User.class);
      System.out.println(user);
    } catch (ClientErrorException primary_exception) {
      try {
        ErrorResponse error_response = primary_exception.getResponse().readEntity(ErrorResponse.class);
        System.out.println(error_response);
      } catch (ClientErrorException secondary_exception) {
        System.err.println("Secondary Exception: " + secondary_exception.getMessage());
        throw new ThisAppException("Secondary Exception", secondary_exception);
      }
    }