Search in sources :

Example 36 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project beam by apache.

the class LatencyRecordingHttpRequestInitializerTest method testErrorResponse.

@Test
public void testErrorResponse() throws IOException {
    when(mockLowLevelRequest.execute()).thenReturn(mockLowLevelResponse);
    when(mockLowLevelResponse.getStatusCode()).thenReturn(403);
    try {
        Storage.Buckets.Get result = storage.buckets().get("test");
        HttpResponse response = result.executeUnparsed();
        assertNotNull(response);
    } catch (HttpResponseException e) {
        assertThat(e.getMessage(), Matchers.containsString("403"));
    }
    verify(mockHistogram, only()).update(anyDouble());
    verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString());
    verify(mockLowLevelRequest).setTimeout(anyInt(), anyInt());
    verify(mockLowLevelRequest).setWriteTimeout(anyInt());
    verify(mockLowLevelRequest).execute();
    verify(mockLowLevelResponse, atLeastOnce()).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 37 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 {
    MockLowLevelHttpResponse[] responses = createMockResponseWithStatusCode(// Non-retryable error.
    403, // Shouldn't happen.
    200);
    when(mockLowLevelRequest.execute()).thenReturn(responses[0], responses[1]);
    try {
        Storage.Buckets.Get result = storage.buckets().get("test");
        HttpResponse response = result.executeUnparsed();
        assertNotNull(response);
    } catch (HttpResponseException e) {
        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).setWriteTimeout(anyInt());
    verify(mockLowLevelRequest).execute();
    verify(responses[0], atLeastOnce()).getStatusCode();
    verify(responses[1], never()).getStatusCode();
    expectedLogs.verifyWarn("Request failed with code 403");
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpResponse(com.google.api.client.http.HttpResponse) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) Test(org.junit.Test)

Example 38 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project zhcet-web by zhcet-amu.

the class FirebaseErrorParsingUtils method getTokenStatus.

/**
 * Checks if a firebase token is invalid by checking if it either expired or not forbidden
 * @param throwable Throwable of firebase execution error
 * @return TokenStatus denoting status of the token
 */
public static TokenStatus getTokenStatus(Throwable throwable) {
    Throwable current = throwable;
    while (!(current instanceof FirebaseMessagingException) && current != null) {
        current = current.getCause();
    }
    if (current == null)
        throw new InvalidThrowableException(throwable);
    // We have a FirebaseMessagingException
    FirebaseMessagingException firebaseMessagingException = (FirebaseMessagingException) current;
    while (!(current instanceof HttpResponseException) && current != null) {
        current = current.getCause();
    }
    if (current == null)
        throw new InvalidThrowableException(throwable);
    // We have a HttpResponseException
    HttpResponseException httpResponseException = (HttpResponseException) current;
    int statusCode = httpResponseException.getStatusCode();
    Reason reason = new Reason(statusCode, current.getMessage(), httpResponseException.getContent());
    boolean isTokenExpired = statusCode == 404 || statusCode == 400;
    boolean isTokenForbidden = statusCode == 403;
    if (isTokenExpired || isTokenForbidden) {
        return new TokenStatus(false, reason);
    } else {
        return new TokenStatus(true, reason);
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) FirebaseMessagingException(com.google.firebase.messaging.FirebaseMessagingException)

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