}
}
private static CloudFoundryException getException(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
CloudFoundryException cloudFoundryException = null;
String description = "Client error";
String statusText = response.getStatusText();
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
if (response.getBody() != null) {
try {
@SuppressWarnings("unchecked")
Map<String, Object> map = mapper.readValue(response.getBody(), Map.class);
description = CloudUtil.parse(String.class, map.get("description"));
int cloudFoundryErrorCode = CloudUtil.parse(Integer.class, map.get("code"));
if (cloudFoundryErrorCode >= 0) {
switch (cloudFoundryErrorCode) {
case StagingErrorException.ERROR_CODE:
cloudFoundryException = new StagingErrorException(
statusCode, statusText);
break;
case NotFinishedStagingException.ERROR_CODE:
cloudFoundryException = new NotFinishedStagingException(
statusCode, statusText);
break;
}
}
} catch (JsonParseException e) {
// Fall through. Handled below.
} catch (IOException e) {
// Fall through. Handled below.
}
}
if (cloudFoundryException == null) {
cloudFoundryException = new CloudFoundryException(statusCode,
statusText);
}
cloudFoundryException.setDescription(description);
return cloudFoundryException;
}