use of com.qcloud.cos.exception.CosServiceExceptionBuilder in project cos-java-sdk-v5 by tencentyun.
the class CosErrorResponseHandler method handle.
@Override
public CosServiceException handle(CosHttpResponse httpResponse) throws XMLStreamException {
final InputStream is = httpResponse.getContent();
String xmlContent = null;
/*
* We don't always get an error response body back from COS. When we send a HEAD request, we
* don't receive a body, so we'll have to just return what we can.
*/
if (is == null || httpResponse.getRequest().getHttpMethod() == HttpMethodName.HEAD) {
return createExceptionFromHeaders(httpResponse, null);
}
String content = null;
try {
content = IOUtils.toString(is);
} catch (IOException ioe) {
log.debug("Failed in parsing the error response : ", ioe);
return createExceptionFromHeaders(httpResponse, null);
}
XMLStreamReader reader;
synchronized (xmlInputFactory) {
reader = xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(content.getBytes(UTF8)));
}
try {
/*
* target depth is to determine if the XML Error response from the server has any
* element inside <Error> tag have child tags. Parsing such tags is not supported now.
* target depth is incremented for every start tag and decremented after every end tag
* is encountered.
*/
int targetDepth = 0;
final CosServiceExceptionBuilder exceptionBuilder = new CosServiceExceptionBuilder();
exceptionBuilder.setErrorResponseXml(content);
exceptionBuilder.setStatusCode(httpResponse.getStatusCode());
boolean hasErrorTagVisited = false;
while (reader.hasNext()) {
int event = reader.next();
switch(event) {
case XMLStreamConstants.START_ELEMENT:
targetDepth++;
String tagName = reader.getLocalName();
if (targetDepth == 1 && !COSErrorTags.Error.toString().equals(tagName))
return createExceptionFromHeaders(httpResponse, "Unable to parse error response. Error XML Not in proper format." + content);
if (COSErrorTags.Error.toString().equals(tagName)) {
hasErrorTagVisited = true;
}
continue;
case XMLStreamConstants.CHARACTERS:
xmlContent = reader.getText();
if (xmlContent != null)
xmlContent = xmlContent.trim();
continue;
case XMLStreamConstants.END_ELEMENT:
tagName = reader.getLocalName();
targetDepth--;
if (!(hasErrorTagVisited) || targetDepth > 1) {
return createExceptionFromHeaders(httpResponse, "Unable to parse error response. Error XML Not in proper format." + content);
}
if (COSErrorTags.Message.toString().equals(tagName)) {
exceptionBuilder.setErrorMessage(xmlContent);
} else if (COSErrorTags.Code.toString().equals(tagName)) {
exceptionBuilder.setErrorCode(xmlContent);
} else if (COSErrorTags.RequestId.toString().equals(tagName)) {
exceptionBuilder.setRequestId(xmlContent);
} else if (COSErrorTags.TraceId.toString().equals(tagName)) {
exceptionBuilder.setTraceId(xmlContent);
} else {
exceptionBuilder.addAdditionalDetail(tagName, xmlContent);
}
continue;
case XMLStreamConstants.END_DOCUMENT:
return exceptionBuilder.build();
}
}
} catch (Exception e) {
log.debug("Failed in parsing the error response : " + content, e);
}
return createExceptionFromHeaders(httpResponse, content);
}
use of com.qcloud.cos.exception.CosServiceExceptionBuilder in project cos-java-sdk-v5 by tencentyun.
the class CosErrorResponseHandler method createExceptionFromHeaders.
private CosServiceException createExceptionFromHeaders(CosHttpResponse errorResponse, String errorResponseXml) {
final Map<String, String> headers = errorResponse.getHeaders();
final int statusCode = errorResponse.getStatusCode();
final CosServiceExceptionBuilder exceptionBuilder = new CosServiceExceptionBuilder();
exceptionBuilder.setErrorMessage(errorResponse.getStatusText());
exceptionBuilder.setErrorResponseXml(errorResponseXml);
exceptionBuilder.setStatusCode(statusCode);
exceptionBuilder.setTraceId(headers.get(Headers.TRACE_ID));
exceptionBuilder.setRequestId(headers.get(Headers.REQUEST_ID));
exceptionBuilder.setErrorCode(statusCode + " " + errorResponse.getStatusText());
return exceptionBuilder.build();
}
Aggregations