下面列出了java.util.concurrent.ConcurrentHashMap.KeySetView#com.google.api.client.googleapis.json.GoogleJsonError 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convert the passed Json "result" to a response.
*
* <p>A {@code null} result will be converted to a successful response. An error response will
* be generated only if the {@code result} is a {@link GoogleJsonError}.
*
* @param result the Json execute result.
* @return the converted response of the result.
* @throws IOException
*/
private MockLowLevelHttpResponse getResponse(GenericJson result) throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
if (result instanceof GoogleJsonError) {
GoogleJsonError error = (GoogleJsonError) result;
String errorContent = JSON_FACTORY.toString(new GenericJson().set("error", error));
response
.setStatusCode(error.getCode())
.setReasonPhrase(error.getMessage())
.setContentType(Json.MEDIA_TYPE)
.setContent(errorContent);
} else {
response
.setStatusCode(HTTP_OK)
.setContentType(Json.MEDIA_TYPE)
.setContent(result == null ? "" : JSON_FACTORY.toString(result));
}
return response;
}
@Test(expected = IOException.class)
public void execute_indexItemNotFound_notPushedToQueue_throwsIOException() throws Exception {
Item item = new Item().setName("id1").setAcl(getCustomerAcl());
RepositoryDoc doc = new RepositoryDoc.Builder().setItem(item).build();
doAnswer(
invocation -> {
SettableFuture<Operation> updateFuture = SettableFuture.create();
updateFuture.setException(
new GoogleJsonResponseException(
new HttpResponseException.Builder(
HTTP_NOT_FOUND, "not found", new HttpHeaders()),
new GoogleJsonError()));
return updateFuture;
})
.when(mockIndexingService)
.indexItem(item, RequestMode.UNSPECIFIED);
try {
doc.execute(mockIndexingService);
} finally {
InOrder inOrder = inOrder(mockIndexingService);
inOrder.verify(mockIndexingService).indexItem(item, RequestMode.UNSPECIFIED);
inOrder.verifyNoMoreInteractions();
assertEquals("id1", doc.getItem().getName());
}
}
private GoogleJsonError getGoogleJsonError(Exception e) {
if (e instanceof GoogleJsonResponseException) {
return ((GoogleJsonResponseException) e).getDetails();
}
boolean retryableException =
(e instanceof SSLHandshakeException || e instanceof SocketTimeoutException);
if (retryableException) {
logger.log(Level.WARNING, "Retrying request failed with exception:", e);
}
// Using retryable 504 Gateway Timeout error code.
int errorCode = retryableException ? 504 : 0;
GoogleJsonError error = new GoogleJsonError();
error.setMessage(e.getMessage());
error.setCode(errorCode);
return error;
}
/**
* Wrapper on {@link JsonBatchCallback#onFailure} to record failure while executing batched
* request.
*/
@Override
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) {
if (event != null) {
event.failure();
event = null;
} else {
operationStats.event(request.requestToExecute.getClass().getName()).failure();
}
logger.log(Level.WARNING, "Request failed with error {0}", error);
if (request.retryPolicy.isRetryableStatusCode(error.getCode())) {
if (request.getRetries() < request.retryPolicy.getMaxRetryLimit()) {
request.setStatus(Status.RETRYING);
request.incrementRetries();
return;
}
}
GoogleJsonResponseException exception =
new GoogleJsonResponseException(
new Builder(error.getCode(), error.getMessage(), responseHeaders), error);
fail(exception);
}
@Test
public void testFailedResult() throws Exception {
BatchRequestService batchService = setupService();
batchService.startAsync().awaitRunning();
assertTrue(batchService.isRunning());
BatchRequest batchRequest = getMockBatchRequest();
when(batchRequestHelper.createBatch(any())).thenReturn(batchRequest);
AsyncRequest<GenericJson> requestToBatch =
new AsyncRequest<GenericJson>(testRequest, retryPolicy, operationStats);
GoogleJsonError error = new GoogleJsonError();
error.setCode(403);
error.setMessage("unauthorized");
doAnswer(
invocation -> {
requestToBatch.getCallback().onFailure(error, new HttpHeaders());
return null;
})
.when(batchRequestHelper)
.executeBatchRequest(batchRequest);
batchService.add(requestToBatch);
batchService.flush();
batchService.stopAsync().awaitTerminated();
validateFailedResult(requestToBatch.getFuture());
assertFalse(batchService.isRunning());
}
@Test
public void testBatchRequestIOException() throws Exception {
BatchRequestService batchService = setupService();
batchService.startAsync().awaitRunning();
assertTrue(batchService.isRunning());
BatchRequest batchRequest = getMockBatchRequest();
when(batchRequestHelper.createBatch(any())).thenReturn(batchRequest);
AsyncRequest<GenericJson> requestToBatch =
new AsyncRequest<GenericJson>(testRequest, retryPolicy, operationStats);
GoogleJsonError error = new GoogleJsonError();
error.setCode(403);
error.setMessage("unauthorized");
doAnswer(
invocation -> {
throw new IOException();
})
.when(batchRequestHelper)
.executeBatchRequest(batchRequest);
batchService.add(requestToBatch);
batchService.flush();
batchService.stopAsync().awaitTerminated();
validateFailedResult(requestToBatch.getFuture());
assertFalse(batchService.isRunning());
}
@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 DefaultHttpResponseExceptionMappingService().map(new org.apache.http.client
.HttpResponseException(response.getStatusCode(), buffer.toString()));
}
return super.map(failure);
}
@Test
public void shouldHandlePermissionDenied() throws IOException {
when(serviceAccountKeyManager.serviceAccountExists(anyString())).thenReturn(true);
final GoogleJsonResponseException permissionDenied = new GoogleJsonResponseException(
new HttpResponseException.Builder(403, "Forbidden", new HttpHeaders()),
new GoogleJsonError().set("status", "PERMISSION_DENIED"));
doThrow(permissionDenied).when(serviceAccountKeyManager).createJsonKey(any());
doThrow(permissionDenied).when(serviceAccountKeyManager).createP12Key(any());
exception.expect(InvalidExecutionException.class);
exception.expectMessage(String.format(
"Permission denied when creating keys for service account: %s. Styx needs to be Service Account Key Admin.",
SERVICE_ACCOUNT));
sut.ensureServiceAccountKeySecret(WORKFLOW_ID.toString(), SERVICE_ACCOUNT);
}
@Test
public void shouldHandleTooManyKeysCreated() throws IOException {
when(serviceAccountKeyManager.serviceAccountExists(anyString())).thenReturn(true);
final GoogleJsonResponseException resourceExhausted = new GoogleJsonResponseException(
new HttpResponseException.Builder(429, "RESOURCE_EXHAUSTED", new HttpHeaders()),
new GoogleJsonError().set("status", "RESOURCE_EXHAUSTED"));
doThrow(resourceExhausted).when(serviceAccountKeyManager).createJsonKey(any());
doThrow(resourceExhausted).when(serviceAccountKeyManager).createP12Key(any());
exception.expect(InvalidExecutionException.class);
exception.expectMessage(String.format(
"Maximum number of keys on service account reached: %s. Styx requires 4 keys to operate.",
SERVICE_ACCOUNT));
sut.ensureServiceAccountKeySecret(WORKFLOW_ID.toString(), SERVICE_ACCOUNT);
}
public static void notifyUserOnError(Context ctx, Exception exc) {
if (exc == null) {
return;
}
final String message;
if (exc instanceof GoogleJsonResponseException) {
GoogleJsonResponseException exception = (GoogleJsonResponseException) exc;
List<GoogleJsonError.ErrorInfo> errors = exception.getDetails().getErrors();
if (errors != null && !errors.isEmpty()) {
message = "Server error:" + errors.get(0).getMessage()+ ", reason: "+ errors.get(0).getReason();
} else {
message = exception.getDetails().getMessage();
}
} else {
message = exc.getMessage();
}
if (message != null) {
Log.e("SkyTubeApp", "Error: "+message);
Toast.makeText(ctx, message, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean delete(Path path, boolean recursive) throws IOException {
Preconditions.checkArgument(recursive, "Non-recursive is not supported.");
String fileId = toFileId(path);
LOG.debug("Deleting file: " + fileId);
try {
client.files().delete(fileId).execute();
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
if (404 == error.getCode()) { //Non-existing file id
return false;
}
throw e;
}
return true;
}
public static MockLowLevelHttpResponse jsonErrorResponse(ErrorResponses errorResponse)
throws IOException {
GoogleJsonError.ErrorInfo errorInfo = new GoogleJsonError.ErrorInfo();
errorInfo.setReason(errorResponse.getErrorReason());
errorInfo.setDomain(errorResponse.getErrorDomain());
errorInfo.setFactory(JSON_FACTORY);
GoogleJsonError jsonError = new GoogleJsonError();
jsonError.setCode(errorResponse.getErrorCode());
jsonError.setErrors(ImmutableList.of(errorInfo));
jsonError.setMessage(errorResponse.getErrorMessage());
jsonError.setFactory(JSON_FACTORY);
GenericJson errorResponseJson = new GenericJson();
errorResponseJson.set("error", jsonError);
errorResponseJson.setFactory(JSON_FACTORY);
return new MockLowLevelHttpResponse()
.setContent(errorResponseJson.toPrettyString())
.setContentType(Json.MEDIA_TYPE)
.setStatusCode(errorResponse.getResponseCode());
}
/** Processes failed copy requests */
private void onCopyFailure(
KeySetView<IOException, Boolean> innerExceptions,
GoogleJsonError jsonError,
HttpHeaders responseHeaders,
String srcBucketName,
String srcObjectName) {
GoogleJsonResponseException cause = createJsonResponseException(jsonError, responseHeaders);
innerExceptions.add(
errorExtractor.itemNotFound(cause)
? createFileNotFoundException(srcBucketName, srcObjectName, cause)
: new IOException(
String.format(
"Error copying '%s'", StringPaths.fromComponents(srcBucketName, srcObjectName)),
cause));
}
protected static void checkGoogleJsonResponseException(GoogleJsonResponseException e)
throws GoogleJsonResponseException {
GoogleJsonError err = e.getDetails();
// err can be null if response is not JSON
if (err != null) {
// For errors in the 4xx range, print out the errors and continue normally.
if (err.getCode() >= 400 && err.getCode() < 500) {
System.out.printf("There are %d error(s)%n", err.getErrors().size());
for (ErrorInfo info : err.getErrors()) {
System.out.printf("- [%s] %s%n", info.getReason(), info.getMessage());
}
} else {
throw e;
}
} else {
throw e;
}
}
/**
* Create an error result when invalid parameters are detected during {@link
* TestingHttpRequest#execute()}.
*
* @param method problem method ("GET", "PUT", etc.).
* @param location problem url.
* @param message error string.
* @return an error result.
*/
private GoogleJsonError badRequest(String method, String location, String message, int code) {
return new GoogleJsonError()
.set("code", code)
.set("message", "Bad Request: " + method)
.set(
"errors",
Collections.singletonList(
new ErrorInfo().set("location", location).set("message", message)));
}
private static <T> ListenableFuture<T> getExceptionFuture(GoogleJsonError e) {
SettableFuture<T> settable = SettableFuture.create();
GoogleJsonResponseException exception =
new GoogleJsonResponseException(
new Builder(e.getCode(), e.getMessage(), new HttpHeaders()), e);
settable.setException(exception);
return settable;
}
@Test(expected = IOException.class)
public void execute_indexFailed_pushedToQueue_throwsIOException() throws Exception {
Item item =
new Item().setName("id1").setQueue("Q1").setPayload("1234").setAcl(getCustomerAcl());
RepositoryDoc doc = new RepositoryDoc.Builder().setItem(item).build();
doAnswer(
invocation -> {
SettableFuture<Operation> updateFuture = SettableFuture.create();
updateFuture.setException(
new GoogleJsonResponseException(
new HttpResponseException.Builder(
HTTP_BAD_GATEWAY, "bad gateway", new HttpHeaders()),
new GoogleJsonError()));
return updateFuture;
})
.when(mockIndexingService)
.indexItem(item, RequestMode.UNSPECIFIED);
when(mockIndexingService.push(anyString(), any()))
.thenReturn(Futures.immediateFuture(new Item()));
try {
doc.execute(mockIndexingService);
} finally {
InOrder inOrder = inOrder(mockIndexingService);
inOrder.verify(mockIndexingService).indexItem(item, RequestMode.UNSPECIFIED);
ArgumentCaptor<PushItem> pushItemArgumentCaptor = ArgumentCaptor.forClass(PushItem.class);
inOrder
.verify(mockIndexingService)
.push(eq(item.getName()), pushItemArgumentCaptor.capture());
PushItem pushItem = pushItemArgumentCaptor.getValue();
assertEquals("Q1", pushItem.getQueue());
assertEquals("SERVER_ERROR", pushItem.getRepositoryError().getType());
assertEquals("1234", pushItem.getPayload());
assertEquals("id1", doc.getItem().getName());
}
}
@Test
public void testFailure() throws IOException {
AsyncRequest<GenericJson> req = new AsyncRequest<>(testRequest, retryPolicy, operationStats);
req.getCallback().onStart();
req.getCallback().onFailure(new GoogleJsonError(), new HttpHeaders());
// Calling failure again should create a new event
req.getCallback().onFailure(new GoogleJsonError(), new HttpHeaders());
verify(operationStats, times(2)).event(testRequest.getClass().getName());
verify(event, times(1)).start();
verify(event, times(2)).failure();
}
@Test
public void testRetryableFailure() throws IOException {
AsyncRequest<GenericJson> req = new AsyncRequest<>(testRequest, retryPolicy, operationStats);
when(retryPolicy.isRetryableStatusCode(anyInt())).thenReturn(true);
req.getCallback().onStart();
req.getCallback().onFailure(new GoogleJsonError(), new HttpHeaders());
req.getCallback().onStart();
req.getCallback().onFailure(new GoogleJsonError(), new HttpHeaders());
req.getCallback().onStart();
req.getCallback().onSuccess(new GenericJson(), new HttpHeaders());
verify(operationStats, times(3)).event(testRequest.getClass().getName());
verify(event, times(3)).start();
verify(event, times(2)).failure();
verify(event, times(1)).success();
}
@Test
public void testFailureUnstarted() throws IOException {
AsyncRequest<GenericJson> req = new AsyncRequest<>(testRequest, retryPolicy, operationStats);
req.getCallback().onFailure(new GoogleJsonError(), new HttpHeaders());
verify(operationStats).event(testRequest.getClass().getName());
verify(event, times(1)).failure();
}
@Test
@SuppressWarnings("unchecked")
public void testAllRetriesFailed() throws Exception {
int httpErrorCode = 503;
when(retryPolicy.isRetryableStatusCode(httpErrorCode)).thenReturn(true);
int retries = 3;
when(retryPolicy.getMaxRetryLimit()).thenReturn(retries);
BatchRequestService batchService = setupService();
batchService.startAsync().awaitRunning();
assertTrue(batchService.isRunning());
BatchRequest batchRequest = getMockBatchRequest();
when(batchRequestHelper.createBatch(any())).thenReturn(batchRequest);
AsyncRequest<GenericJson> requestToBatch =
new AsyncRequest<GenericJson>(testRequest, retryPolicy, operationStats);
assertEquals(0, requestToBatch.getRetries());
assertEquals(Status.NEW, requestToBatch.getStatus());
GoogleJsonError error = new GoogleJsonError();
error.setCode(httpErrorCode);
error.setMessage("Service Unavailable");
doAnswer(
i -> {
requestToBatch.getCallback().onFailure(error, new HttpHeaders());
return null;
})
.when(batchRequestHelper)
.executeBatchRequest(batchRequest);
batchService.add(requestToBatch);
batchService.flush();
batchService.stopAsync().awaitTerminated();
verify(retryPolicy, times(retries + 1)).isRetryableStatusCode(httpErrorCode);
verify(retryPolicy, times(retries + 1)).getMaxRetryLimit();
validateFailedResult(requestToBatch.getFuture());
assertEquals(Status.FAILED, requestToBatch.getStatus());
assertEquals(retries, requestToBatch.getRetries());
assertFalse(batchService.isRunning());
}
@Test
@SuppressWarnings("unchecked")
public void testNonRetryableErrorCode() throws Exception {
int httpErrorCode = 65535;
when(retryPolicy.isRetryableStatusCode(httpErrorCode)).thenReturn(false);
BatchRequestService batchService = setupService();
batchService.startAsync().awaitRunning();
assertTrue(batchService.isRunning());
BatchRequest batchRequest = getMockBatchRequest();
when(batchRequestHelper.createBatch(any())).thenReturn(batchRequest);
AsyncRequest<GenericJson> requestToBatch =
new AsyncRequest<GenericJson>(testRequest, retryPolicy, operationStats);
assertEquals(0, requestToBatch.getRetries());
assertEquals(Status.NEW, requestToBatch.getStatus());
GoogleJsonError error = new GoogleJsonError();
error.setCode(httpErrorCode);
error.setMessage("Unknown error code");
doAnswer(
i -> {
requestToBatch.getCallback().onFailure(error, new HttpHeaders());
return null;
})
.when(batchRequestHelper)
.executeBatchRequest(batchRequest);
batchService.add(requestToBatch);
batchService.flush();
batchService.stopAsync().awaitTerminated();
verify(retryPolicy).isRetryableStatusCode(httpErrorCode);
validateFailedResult(requestToBatch.getFuture());
assertEquals(Status.FAILED, requestToBatch.getStatus());
assertEquals(0, requestToBatch.getRetries());
assertFalse(batchService.isRunning());
}
@Override
public BackgroundException map(final IOException failure) {
final StringBuilder buffer = new StringBuilder();
if(failure instanceof GoogleJsonResponseException) {
final GoogleJsonResponseException error = (GoogleJsonResponseException) failure;
if(error.getDetails() != null) {
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 DefaultHttpResponseExceptionMappingService().map(new org.apache.http.client
.HttpResponseException(response.getStatusCode(), buffer.toString()));
}
return super.map(failure);
}
@Test
public void notFoundResponseIsNotPermissionDenied() {
assertThat(GcpUtil.isPermissionDenied(new GoogleJsonResponseException(
new HttpResponseException.Builder(404, "Not Found", new HttpHeaders()), new GoogleJsonError())), is(false));
assertThat(GcpUtil.isPermissionDenied(new GoogleJsonResponseException(
new HttpResponseException.Builder(404, "Not Found", new HttpHeaders()), null)), is(false));
}
@Test
public void notFoundResponseIsNotPResourceExhausted() {
assertThat(GcpUtil.isResourceExhausted(new GoogleJsonResponseException(
new HttpResponseException.Builder(404, "Not Found", new HttpHeaders()), new GoogleJsonError())), is(false));
assertThat(GcpUtil.isResourceExhausted(new GoogleJsonResponseException(
new HttpResponseException.Builder(404, "Not Found", new HttpHeaders()), null)), is(false));
}
/**
* Uses the provided specification for the staging location, creating it if it does not already
* exist. This may be a long-running blocking operation.
*/
public StagingLocationVerificationResult createStagingLocation(
String projectId, String stagingLocation, IProgressMonitor progressMonitor) {
SubMonitor monitor = SubMonitor.convert(progressMonitor, 2);
String bucketName = toGcsBucketName(stagingLocation);
if (locationIsAccessible(stagingLocation)) { // bucket already exists
return new StagingLocationVerificationResult(
String.format("Bucket %s exists", bucketName), true);
}
monitor.worked(1);
// else create the bucket
try {
Bucket newBucket = new Bucket();
newBucket.setName(bucketName);
gcsClient.buckets().insert(projectId, newBucket).execute();
return new StagingLocationVerificationResult(
String.format("Bucket %s created", bucketName), true);
} catch (GoogleJsonResponseException ex) {
GoogleJsonError error = ex.getDetails();
return new StagingLocationVerificationResult(error.getMessage(), false);
} catch (IOException e) {
return new StagingLocationVerificationResult(e.getMessage(), false);
} finally {
monitor.done();
}
}
private static GoogleJsonResponseException fakeGoogleJsonResponseException(
int status, ErrorInfo errorInfo, String message) throws IOException {
final JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
errorInfo.setFactory(jsonFactory);
GoogleJsonError jsonError = new GoogleJsonError();
jsonError.setCode(status);
jsonError.setErrors(Collections.singletonList(errorInfo));
jsonError.setMessage(message);
jsonError.setFactory(jsonFactory);
GenericJson errorResponse = new GenericJson();
errorResponse.set("error", jsonError);
errorResponse.setFactory(jsonFactory);
return new MockLowLevelHttpRequest()
.setResponse(
new MockLowLevelHttpResponse()
.setContent(errorResponse.toPrettyString())
.setContentType(Json.MEDIA_TYPE)
.setStatusCode(status));
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
return GoogleJsonResponseException.from(jsonFactory, response);
}
/** A helper that generates the error JSON payload that Google APIs produce. */
private static GoogleJsonErrorContainer errorWithReasonAndStatus(String reason, int status) {
ErrorInfo info = new ErrorInfo();
info.setReason(reason);
info.setDomain("global");
// GoogleJsonError contains one or more ErrorInfo objects; our utiities read the first one.
GoogleJsonError error = new GoogleJsonError();
error.setErrors(ImmutableList.of(info));
error.setCode(status);
error.setMessage(reason);
// The actual JSON response is an error container.
GoogleJsonErrorContainer container = new GoogleJsonErrorContainer();
container.setError(error);
return container;
}
/** Create an error for JSON server response errors. */
public static BigqueryJobFailureException create(GoogleJsonResponseException cause) {
GoogleJsonError err = cause.getDetails();
if (err != null) {
return new BigqueryJobFailureException(err.getMessage(), null, null, err);
} else {
return new BigqueryJobFailureException(cause.getMessage(), cause, null, null);
}
}
public BigqueryJobFailureException(
String message,
@Nullable Throwable cause,
@Nullable JobStatus jobStatus,
@Nullable GoogleJsonError jsonError) {
super(message, cause);
this.jobStatus = jobStatus;
this.jsonError = jsonError;
}