use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.
the class BaseHttpServiceException method makeExceptionData.
private static ExceptionData makeExceptionData(IOException exception, boolean idempotent, Set<BaseServiceException.Error> retryableErrors) {
int code = UNKNOWN_CODE;
String reason = null;
String location = null;
String debugInfo = null;
Boolean retryable = null;
if (exception instanceof HttpResponseException) {
if (exception instanceof GoogleJsonResponseException) {
GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails();
if (jsonError != null) {
BaseServiceException.Error error = new BaseServiceException.Error(jsonError.getCode(), reason(jsonError));
code = error.getCode();
reason = error.getReason();
retryable = error.isRetryable(idempotent, retryableErrors);
if (reason != null) {
GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0);
location = errorInfo.getLocation();
debugInfo = (String) errorInfo.get("debugInfo");
}
} else {
code = ((GoogleJsonResponseException) exception).getStatusCode();
}
} else {
// In cases where an exception is an instance of HttpResponseException but not
// an instance of GoogleJsonResponseException, check the status code to determine whether it's retryable
code = ((HttpResponseException) exception).getStatusCode();
retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors);
}
}
return ExceptionData.newBuilder().setMessage(message(exception)).setCause(exception).setRetryable(MoreObjects.firstNonNull(retryable, BaseServiceException.isRetryable(idempotent, exception))).setCode(code).setReason(reason).setLocation(location).setDebugInfo(debugInfo).build();
}
use of com.google.api.client.http.HttpResponseException in project beam by apache.
the class RetryHttpRequestInitializerTest method testErrorCodeForbidden.
/**
* Tests that a non-retriable error is not retried.
*/
@Test
public void testErrorCodeForbidden() throws IOException {
when(mockLowLevelRequest.execute()).thenReturn(mockLowLevelResponse);
when(mockLowLevelResponse.getStatusCode()).thenReturn(// Non-retryable error.
403).thenReturn(// Shouldn't happen.
200);
try {
Storage.Buckets.Get result = storage.buckets().get("test");
HttpResponse response = result.executeUnparsed();
assertNotNull(response);
} catch (HttpResponseException e) {
Assert.assertThat(e.getMessage(), Matchers.containsString("403"));
}
verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class));
verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString());
verify(mockLowLevelRequest).setTimeout(anyInt(), anyInt());
verify(mockLowLevelRequest).execute();
verify(mockLowLevelResponse).getStatusCode();
}
use of com.google.api.client.http.HttpResponseException in project google-api-java-client by google.
the class MediaHttpDownloaderTest method testDirectDownloadServerErrorWithBackOffDisabled.
public void testDirectDownloadServerErrorWithBackOffDisabled() throws Exception {
int contentLength = MediaHttpDownloader.MAXIMUM_CHUNK_SIZE * 2;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MediaTransport fakeTransport = new MediaTransport(contentLength);
fakeTransport.directDownloadEnabled = true;
fakeTransport.testServerError = true;
MediaHttpDownloader downloader = new MediaHttpDownloader(fakeTransport, null);
try {
downloader.download(new GenericUrl(TEST_REQUEST_URL), outputStream);
fail("Expected " + HttpResponseException.class);
} catch (HttpResponseException e) {
// Expected
}
// should be 1 call made: 1 download request with server error
assertEquals(1, fakeTransport.lowLevelExecCalls);
}
use of com.google.api.client.http.HttpResponseException in project google-api-java-client by google.
the class MediaHttpDownloaderTest method testDownloadServerErrorWithBackOffDisabled.
public void testDownloadServerErrorWithBackOffDisabled() throws Exception {
int contentLength = MediaHttpDownloader.MAXIMUM_CHUNK_SIZE * 2;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MediaTransport fakeTransport = new MediaTransport(contentLength);
fakeTransport.testServerError = true;
MediaHttpDownloader downloader = new MediaHttpDownloader(fakeTransport, null);
try {
downloader.download(new GenericUrl(TEST_REQUEST_URL), outputStream);
fail("Expected " + HttpResponseException.class);
} catch (HttpResponseException e) {
// Expected
}
// There should be 2 calls made: 1 successful download request and 1 download request with
// server error.
assertEquals(2, fakeTransport.lowLevelExecCalls);
}
use of com.google.api.client.http.HttpResponseException in project data-transfer-project by google.
the class SmugMugInterface method postRequest.
<T> T postRequest(String url, HttpContent content, Map<String, String> headers, TypeReference<T> typeReference) throws IOException {
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
String fullUrl = url;
if (!fullUrl.contains("://")) {
fullUrl = BASE_URL + url;
}
HttpRequest postRequest = requestFactory.buildPostRequest(new GenericUrl(fullUrl), content);
HttpHeaders httpHeaders = new HttpHeaders().setAccept("application/json").setContentType("application/json");
for (Entry<String, String> entry : headers.entrySet()) {
httpHeaders.put(entry.getKey(), entry.getValue());
}
postRequest.setHeaders(httpHeaders);
// TODO(olsona): sign request
HttpResponse response;
try {
response = postRequest.execute();
} catch (HttpResponseException e) {
throw new IOException("Problem making request: " + postRequest.getUrl(), e);
}
int statusCode = response.getStatusCode();
if (statusCode < 200 || statusCode >= 300) {
throw new IOException("Bad status code: " + statusCode + " error: " + response.getStatusMessage());
}
String result = CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8));
return MAPPER.readValue(result, typeReference);
}
Aggregations