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();
}
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");
}
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);
}
}
Aggregations