本文整理匯總了Java中com.google.api.client.http.HttpResponseException類的典型用法代碼示例。如果您正苦於以下問題:Java HttpResponseException類的具體用法?Java HttpResponseException怎麽用?Java HttpResponseException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpResponseException類屬於com.google.api.client.http包,在下文中一共展示了HttpResponseException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testGetUserUnexpectedHttpError
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void testGetUserUnexpectedHttpError() throws Exception {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setContent("{\"not\" json}");
response.setStatusCode(500);
MockHttpTransport transport = new MockHttpTransport.Builder()
.setLowLevelHttpResponse(response)
.build();
FirebaseUserManager userManager = new FirebaseUserManager(gson, transport, credentials);
try {
userManager.getUserById("testuser");
fail("No error thrown for JSON error");
} catch (FirebaseAuthException e) {
assertTrue(e.getCause() instanceof HttpResponseException);
assertEquals("Unexpected HTTP response with status: 500; body: {\"not\" json}",
e.getMessage());
assertEquals(FirebaseUserManager.INTERNAL_ERROR, e.getErrorCode());
}
}
示例2: registerMetric_fetchesStackdriverDefinition
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void registerMetric_fetchesStackdriverDefinition() throws Exception {
// Stackdriver throws an Exception with the status message "ALREADY_EXISTS" when you try to
// register a metric that's already been registered, so we fake one here.
ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8));
HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream);
HttpResponseException.Builder httpResponseExceptionBuilder =
new HttpResponseException.Builder(response);
httpResponseExceptionBuilder.setStatusCode(400);
httpResponseExceptionBuilder.setStatusMessage("ALREADY_EXISTS");
GoogleJsonResponseException exception =
new GoogleJsonResponseException(httpResponseExceptionBuilder, null);
when(metricDescriptorCreate.execute()).thenThrow(exception);
StackdriverWriter writer =
new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
writer.registerMetric(metric);
verify(client.projects().metricDescriptors().get("metric")).execute();
}
示例3: registerMetric_rethrowsException
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void registerMetric_rethrowsException() throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8));
HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream);
HttpResponseException.Builder httpResponseExceptionBuilder =
new HttpResponseException.Builder(response);
httpResponseExceptionBuilder.setStatusCode(404);
GoogleJsonResponseException exception =
new GoogleJsonResponseException(httpResponseExceptionBuilder, null);
when(metricDescriptorCreate.execute()).thenThrow(exception);
StackdriverWriter writer =
new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
assertThrows(GoogleJsonResponseException.class, () -> writer.registerMetric(metric));
assertThat(exception.getStatusCode()).isEqualTo(404);
}
示例4: getEncodedTimeSeries_nullLabels_encodes
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void getEncodedTimeSeries_nullLabels_encodes() throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8));
HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream);
HttpResponseException.Builder httpResponseExceptionBuilder =
new HttpResponseException.Builder(response);
httpResponseExceptionBuilder.setStatusCode(400);
httpResponseExceptionBuilder.setStatusMessage("ALREADY_EXISTS");
GoogleJsonResponseException exception =
new GoogleJsonResponseException(httpResponseExceptionBuilder, null);
when(metricDescriptorCreate.execute()).thenThrow(exception);
when(metricDescriptorGet.execute())
.thenReturn(new MetricDescriptor().setName("foo").setLabels(null));
StackdriverWriter writer =
new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
writer.registerMetric(metric);
TimeSeries timeSeries =
writer.getEncodedTimeSeries(
MetricPoint.create(metric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), 10L));
assertThat(timeSeries.getMetric().getLabels()).isEmpty();
}
示例5: testPush_missingBlobs
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void testPush_missingBlobs() throws IOException, RegistryException {
RegistryClient registryClient = new RegistryClient(null, "gcr.io", "distroless/java");
ManifestTemplate manifestTemplate = registryClient.pullManifest("latest");
registryClient = new RegistryClient(null, "localhost:5000", "busybox");
try {
registryClient.pushManifest((V22ManifestTemplate) manifestTemplate, "latest");
Assert.fail("Pushing manifest without its BLOBs should fail");
} catch (RegistryErrorException ex) {
HttpResponseException httpResponseException = (HttpResponseException) ex.getCause();
Assert.assertEquals(
HttpStatusCodes.STATUS_CODE_BAD_REQUEST, httpResponseException.getStatusCode());
}
}
開發者ID:GoogleCloudPlatform,項目名稱:minikube-build-tools-for-java,代碼行數:17,代碼來源:ManifestPusherIntegrationTest.java
示例6: testHandleHttpResponseException
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void testHandleHttpResponseException() throws IOException, RegistryErrorException {
HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
Mockito.when(mockHttpResponseException.getStatusCode())
.thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
ErrorResponseTemplate emptyErrorResponseTemplate =
new ErrorResponseTemplate()
.addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message"));
Mockito.when(mockHttpResponseException.getContent())
.thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate)));
BlobDescriptor blobDescriptor =
testBlobChecker.handleHttpResponseException(mockHttpResponseException);
Assert.assertNull(blobDescriptor);
}
示例7: testHandleHttpResponseException_hasOtherErrors
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void testHandleHttpResponseException_hasOtherErrors()
throws IOException, RegistryErrorException {
HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
Mockito.when(mockHttpResponseException.getStatusCode())
.thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
ErrorResponseTemplate emptyErrorResponseTemplate =
new ErrorResponseTemplate()
.addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message"))
.addError(new ErrorEntryTemplate(ErrorCodes.MANIFEST_UNKNOWN.name(), "some message"));
Mockito.when(mockHttpResponseException.getContent())
.thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate)));
try {
testBlobChecker.handleHttpResponseException(mockHttpResponseException);
Assert.fail("Non-BLOB_UNKNOWN errors should not be handled");
} catch (HttpResponseException ex) {
Assert.assertEquals(mockHttpResponseException, ex);
}
}
示例8: testHandleHttpResponseException_notBlobUnknown
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void testHandleHttpResponseException_notBlobUnknown()
throws IOException, RegistryErrorException {
HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
Mockito.when(mockHttpResponseException.getStatusCode())
.thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate();
Mockito.when(mockHttpResponseException.getContent())
.thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate)));
try {
testBlobChecker.handleHttpResponseException(mockHttpResponseException);
Assert.fail("Non-BLOB_UNKNOWN errors should not be handled");
} catch (HttpResponseException ex) {
Assert.assertEquals(mockHttpResponseException, ex);
}
}
示例9: tsetHandleHttpResponseException_noHeader
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void tsetHandleHttpResponseException_noHeader() throws HttpResponseException {
Mockito.when(mockHttpResponseException.getStatusCode())
.thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
Mockito.when(mockHttpResponseException.getHeaders()).thenReturn(mockHeaders);
Mockito.when(mockHeaders.getAuthenticate()).thenReturn(null);
try {
testAuthenticationMethodRetriever.handleHttpResponseException(mockHttpResponseException);
Assert.fail(
"Authentication method retriever should fail if 'WWW-Authenticate' header is not found");
} catch (RegistryErrorException ex) {
Assert.assertThat(
ex.getMessage(), CoreMatchers.containsString("'WWW-Authenticate' header not found"));
}
}
開發者ID:GoogleCloudPlatform,項目名稱:minikube-build-tools-for-java,代碼行數:18,代碼來源:AuthenticationMethodRetrieverTest.java
示例10: testHandleHttpResponseException_badAuthenticationMethod
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void testHandleHttpResponseException_badAuthenticationMethod()
throws HttpResponseException {
String authenticationMethod = "bad authentication method";
Mockito.when(mockHttpResponseException.getStatusCode())
.thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
Mockito.when(mockHttpResponseException.getHeaders()).thenReturn(mockHeaders);
Mockito.when(mockHeaders.getAuthenticate()).thenReturn(authenticationMethod);
try {
testAuthenticationMethodRetriever.handleHttpResponseException(mockHttpResponseException);
Assert.fail(
"Authentication method retriever should fail if 'WWW-Authenticate' header failed to parse");
} catch (RegistryErrorException ex) {
Assert.assertThat(
ex.getMessage(),
CoreMatchers.containsString(
"Failed get authentication method from 'WWW-Authenticate' header"));
}
}
開發者ID:GoogleCloudPlatform,項目名稱:minikube-build-tools-for-java,代碼行數:23,代碼來源:AuthenticationMethodRetrieverTest.java
示例11: testHandleHttpResponseException_pass
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Test
public void testHandleHttpResponseException_pass()
throws RegistryErrorException, HttpResponseException, MalformedURLException {
String authenticationMethod =
"Bearer realm=\"https://somerealm\",service=\"someservice\",scope=\"somescope\"";
Mockito.when(mockHttpResponseException.getStatusCode())
.thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
Mockito.when(mockHttpResponseException.getHeaders()).thenReturn(mockHeaders);
Mockito.when(mockHeaders.getAuthenticate()).thenReturn(authenticationMethod);
RegistryAuthenticator registryAuthenticator =
testAuthenticationMethodRetriever.handleHttpResponseException(mockHttpResponseException);
Assert.assertEquals(
new URL("https://somerealm?service=someservice&scope=repository:someImageName:pull"),
registryAuthenticator.getAuthenticationUrl());
}
開發者ID:GoogleCloudPlatform,項目名稱:minikube-build-tools-for-java,代碼行數:19,代碼來源:AuthenticationMethodRetrieverTest.java
示例12: map
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
@Override
public BackgroundException map(final IOException failure) {
final StringBuilder buffer = new StringBuilder();
if(failure instanceof GoogleJsonResponseException) {
final GoogleJsonResponseException error = (GoogleJsonResponseException) failure;
this.append(buffer, error.getDetails().getMessage());
switch(error.getDetails().getCode()) {
case HttpStatus.SC_FORBIDDEN:
final List<GoogleJsonError.ErrorInfo> errors = error.getDetails().getErrors();
for(GoogleJsonError.ErrorInfo info : errors) {
if("usageLimits".equals(info.getDomain())) {
return new RetriableAccessDeniedException(buffer.toString(), Duration.ofSeconds(5), failure);
}
}
break;
}
}
if(failure instanceof HttpResponseException) {
final HttpResponseException response = (HttpResponseException) failure;
this.append(buffer, response.getStatusMessage());
return new HttpResponseExceptionMappingService().map(new org.apache.http.client
.HttpResponseException(response.getStatusCode(), buffer.toString()));
}
return super.map(failure);
}
示例13: convertException
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
/**
* @deprecated As of xero-java-sdk version 0.6.0, to be removed
* in favour of {@link XeroExceptionHandler#convertException(IOException)}
*/
protected RuntimeException convertException(IOException ioe) {
if (ioe instanceof HttpResponseException) {
HttpResponseException googleException = (HttpResponseException) ioe;
if (googleException.getStatusCode() == 400 ||
googleException.getStatusCode() == 401 ||
googleException.getStatusCode() == 404 ||
googleException.getStatusCode() == 500 ||
googleException.getStatusCode() == 503) {
return newApiException(googleException);
} else {
return newApiException(googleException);
}
}
return new RuntimeException(ioe);
}
示例14: execute
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
public boolean execute() throws IOException {
try {
HttpResponse response = request.execute();
isSuccess = response.isSuccessStatusCode();
if (isSuccess) {
Map<String, String> oauthKeys = getQueryMap(response.parseAsString());
this.token = oauthKeys.get("oauth_token");
this.tokenSecret = oauthKeys.get("oauth_token_secret");
this.sessionHandle = oauthKeys.get("oauth_session_handle");
this.tokenTimestamp = System.currentTimeMillis() / 1000l;
isSuccess = true;
} else {
}
} catch (HttpResponseException e) {
Map<String, String> oauthError = getQueryMap(e.getMessage());
this.problem = oauthError.get("oauth_problem");
this.advice = oauthError.get("oauth_problem_advice");
isSuccess = false;
}
return isSuccess;
}
示例15: convertException
import com.google.api.client.http.HttpResponseException; //導入依賴的package包/類
/**
* For backwards comparability with xero-java-sdk version 0.6.0 keep the old way of handling exceptions
*
* @param ioe exception to convert
* @return the converted exception
*/
public XeroApiException convertException(IOException ioe) {
if (ioe instanceof HttpResponseException) {
HttpResponseException httpResponseException = (HttpResponseException) ioe;
if (httpResponseException.getStatusCode() == 400) {
return handleBadRequest(httpResponseException);
} else if (httpResponseException.getStatusCode() == 401 ||
httpResponseException.getStatusCode() == 404 ||
httpResponseException.getStatusCode() == 500 ||
httpResponseException.getStatusCode() == 503) {
return newApiException(httpResponseException);
} else {
return newApiException(httpResponseException);
}
}
throw new XeroClientException(ioe.getMessage(), ioe);
}