Search in sources :

Example 26 with HttpResponseException

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();
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) BaseServiceException(com.google.cloud.BaseServiceException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) HttpResponseException(com.google.api.client.http.HttpResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 27 with HttpResponseException

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();
}
Also used : HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) Test(org.junit.Test)

Example 28 with HttpResponseException

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);
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericUrl(com.google.api.client.http.GenericUrl)

Example 29 with HttpResponseException

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);
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericUrl(com.google.api.client.http.GenericUrl)

Example 30 with HttpResponseException

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);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) InputStreamReader(java.io.InputStreamReader) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException)

Aggregations

HttpResponseException (com.google.api.client.http.HttpResponseException)38 GenericUrl (com.google.api.client.http.GenericUrl)15 IOException (java.io.IOException)15 HttpResponse (com.google.api.client.http.HttpResponse)13 Test (org.junit.Test)13 HttpRequest (com.google.api.client.http.HttpRequest)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)6 HttpHeaders (com.google.api.client.http.HttpHeaders)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)4 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)3 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PServiceCall (net.morimekta.providence.PServiceCall)3 Request (net.morimekta.test.providence.client.Request)3 TestService (net.morimekta.test.providence.client.TestService)3 AccountManager (android.accounts.AccountManager)2 ByteArrayContent (com.google.api.client.http.ByteArrayContent)2 HttpTransport (com.google.api.client.http.HttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2