final InputStream is = errorResponse.getContent();
if (is == null
|| errorResponse.getRequest().getHttpMethod() == HttpMethodName.HEAD) {
String requestId = errorResponse.getHeaders().get(Headers.REQUEST_ID);
String extendedRequestId = errorResponse.getHeaders().get(Headers.EXTENDED_REQUEST_ID);
AmazonS3Exception ase = new AmazonS3Exception(errorResponse.getStatusText());
final int statusCode = errorResponse.getStatusCode();
ase.setStatusCode(statusCode);
ase.setRequestId(requestId);
ase.setExtendedRequestId(extendedRequestId);
ase.setErrorType(errorTypeOf(statusCode));
return ase;
}
// Try to read the error response
String content = "";
try {
content = IOUtils.toString(is);
} catch(IOException ex) {
if (log.isDebugEnabled())
log.debug("Failed in reading the error response", ex);
return newAmazonS3Exception(errorResponse.getStatusText(), errorResponse);
}
try { // try to parse the error response as XML
final Document document = XpathUtils.documentFrom(content);
XPath xpath = xpath();
final String message = asString("Error/Message", document, xpath);
final String errorCode = asString("Error/Code", document, xpath);
final String requestId = asString("Error/RequestId", document, xpath);
final String extendedRequestId = asString("Error/HostId", document, xpath);
final AmazonS3Exception ase = new AmazonS3Exception(message);
final int statusCode = errorResponse.getStatusCode();
ase.setStatusCode(statusCode);
ase.setErrorType(errorTypeOf(statusCode));
ase.setErrorCode(errorCode);
ase.setRequestId(requestId);
ase.setExtendedRequestId(extendedRequestId);
return ase;
} catch(Exception ex) {
if (log.isDebugEnabled())
log.debug("Failed in parsing the response as XML: " + content, ex);
return newAmazonS3Exception(content, errorResponse);