refactored aws error handler to provide the http content even when it isn't parsable (ex. returned in html

This commit is contained in:
Adrian Cole 2010-11-07 01:50:07 +01:00
parent 02d1ec664e
commit 8849e2793a
1 changed files with 31 additions and 31 deletions

View File

@ -66,14 +66,13 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
public void handleError(HttpCommand command, HttpResponse response) { public void handleError(HttpCommand command, HttpResponse response) {
HttpRequest request = command.getRequest(); HttpRequest request = command.getRequest();
Exception exception = new HttpResponseException(command, response); Exception exception = null;
try { try {
AWSError error = null; AWSError error = null;
String message = null; String message = null;
if (response.getPayload() != null) { if (response.getPayload() != null) {
String contentType = response.getPayload().getContentMetadata().getContentType(); String contentType = response.getPayload().getContentMetadata().getContentType();
if (contentType != null if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)) {
&& (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)) {
error = utils.parseAWSErrorFromContent(request, response); error = utils.parseAWSErrorFromContent(request, response);
if (error != null) { if (error != null) {
message = error.getMessage(); message = error.getMessage();
@ -86,36 +85,37 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
} }
} }
} }
message = message != null ? message : String.format("%s -> %s", request.getRequestLine(), response message = message != null ? message : String.format("%s -> %s", request.getRequestLine(),
.getStatusLine()); response.getStatusLine());
if (exception == null)
exception = new HttpResponseException(command, response, message);
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 400: case 400:
if (error != null && error.getCode() != null if (error != null && error.getCode() != null
&& (error.getCode().endsWith(".NotFound") || error.getCode().endsWith(".Unknown"))) && (error.getCode().endsWith(".NotFound") || error.getCode().endsWith(".Unknown")))
exception = new ResourceNotFoundException(message, exception); exception = new ResourceNotFoundException(message, exception);
else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error
.getCode().endsWith(".Duplicate"))) .getCode().endsWith(".Duplicate"))) || (message != null && message.indexOf("already exists") != -1))
|| (message != null && message.indexOf("already exists") != -1)) exception = new IllegalStateException(message, exception);
exception = new IllegalStateException(message, exception); else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure"))
else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure"))
exception = new AuthorizationException(command.getRequest(), message);
else if (message != null && message.indexOf("Failed to bind the following fields") != -1)// Nova
exception = new IllegalArgumentException(message, exception);
break;
case 401:
case 403:
exception = new AuthorizationException(command.getRequest(), message); exception = new AuthorizationException(command.getRequest(), message);
break; else if (message != null && message.indexOf("Failed to bind the following fields") != -1)// Nova
case 404: exception = new IllegalArgumentException(message, exception);
if (!command.getRequest().getMethod().equals("DELETE")) { break;
String container = request.getEndpoint().getHost(); case 401:
String key = request.getEndpoint().getPath(); case 403:
if (key == null || key.equals("/")) exception = new AuthorizationException(command.getRequest(), message);
exception = new ContainerNotFoundException(container, message); break;
else case 404:
exception = new KeyNotFoundException(container, key, message); if (!command.getRequest().getMethod().equals("DELETE")) {
} String container = request.getEndpoint().getHost();
break; String key = request.getEndpoint().getPath();
if (key == null || key.equals("/"))
exception = new ContainerNotFoundException(container, message);
else
exception = new KeyNotFoundException(container, key, message);
}
break;
} }
} finally { } finally {
releasePayload(response); releasePayload(response);