Java Code Examples for com.google.api.client.googleapis.json.GoogleJsonError#setMessage()
The following examples show how to use
com.google.api.client.googleapis.json.GoogleJsonError#setMessage() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatchRequestService.java From connector-sdk with Apache License 2.0 | 6 votes |
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; }
Example 2
Source File: BatchRequestServiceTest.java From connector-sdk with Apache License 2.0 | 6 votes |
@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()); }
Example 3
Source File: BatchRequestServiceTest.java From connector-sdk with Apache License 2.0 | 6 votes |
@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()); }
Example 4
Source File: MockHttpTransportHelper.java From hadoop-connectors with Apache License 2.0 | 6 votes |
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()); }
Example 5
Source File: BatchRequestServiceTest.java From connector-sdk with Apache License 2.0 | 5 votes |
@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()); }
Example 6
Source File: BatchRequestServiceTest.java From connector-sdk with Apache License 2.0 | 5 votes |
@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()); }
Example 7
Source File: CoreSocketFactoryTest.java From cloud-sql-jdbc-socket-factory with Apache License 2.0 | 5 votes |
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); }
Example 8
Source File: BigQueryServicesImplTest.java From beam with Apache License 2.0 | 5 votes |
/** 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; }
Example 9
Source File: ApiErrorExtractorTest.java From hadoop-connectors with Apache License 2.0 | 5 votes |
private static GoogleJsonResponseException googleJsonResponseException( int status, ErrorInfo errorInfo, String httpStatusString) 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(httpStatusString); 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); }
Example 10
Source File: BatchRequestTest.java From google-api-java-client with Apache License 2.0 | 5 votes |
@Override public void onFailure(ErrorOutput.ErrorBody e, HttpHeaders responseHeaders) throws IOException { GoogleJsonErrorContainer errorContainer = new GoogleJsonErrorContainer(); if (e.hasError()) { ErrorOutput.ErrorProto errorProto = e.getError(); GoogleJsonError error = new GoogleJsonError(); if (errorProto.hasCode()) { error.setCode(errorProto.getCode()); } if (errorProto.hasMessage()) { error.setMessage(errorProto.getMessage()); } List<ErrorInfo> errorInfos = new ArrayList<ErrorInfo>(errorProto.getErrorsCount()); for (ErrorOutput.IndividualError individualError : errorProto.getErrorsList()) { ErrorInfo errorInfo = new ErrorInfo(); if (individualError.hasDomain()) { errorInfo.setDomain(individualError.getDomain()); } if (individualError.hasMessage()) { errorInfo.setMessage(individualError.getMessage()); } if (individualError.hasReason()) { errorInfo.setReason(individualError.getReason()); } errorInfos.add(errorInfo); } error.setErrors(errorInfos); errorContainer.setError(error); } callback.onFailure(errorContainer, responseHeaders); }
Example 11
Source File: BatchRequestServiceTest.java From connector-sdk with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testMaximumBackOffTimeReachedWhileRetryingFailedRequests() throws Exception { int httpErrorCode = 503; when(backOff.nextBackOffMillis()).thenReturn(2L).thenReturn(BackOff.STOP); when(retryPolicy.isRetryableStatusCode(httpErrorCode)).thenReturn(true); when(retryPolicy.getMaxRetryLimit()).thenReturn(3); BatchRequestService batchService = setupService(); batchService.startAsync().awaitRunning(); BatchRequest batchRequest = getMockBatchRequest(); when(batchRequestHelper.createBatch(any())).thenReturn(batchRequest); AsyncRequest<GenericJson> requestToBatchSuccessful = new AsyncRequest<GenericJson>(testRequest, retryPolicy, operationStats); AsyncRequest<GenericJson> requestToBatchFailed = new AsyncRequest<GenericJson>(testRequest, retryPolicy, operationStats); assertEquals(0, requestToBatchSuccessful.getRetries()); assertEquals(Status.NEW, requestToBatchSuccessful.getStatus()); GenericJson successfulResult = new GenericJson(); GoogleJsonError error = new GoogleJsonError(); error.setCode(httpErrorCode); error.setMessage("Service Unavailable"); doAnswer( i -> { if (requestToBatchSuccessful.getRetries() >= 1) { requestToBatchSuccessful.getCallback().onStart(); requestToBatchSuccessful .getCallback() .onSuccess(successfulResult, new HttpHeaders()); } else { requestToBatchSuccessful.getCallback().onStart(); requestToBatchSuccessful.getCallback().onFailure(error, new HttpHeaders()); } return null; }) .when(batchRequestHelper) .executeBatchRequest(batchRequest); batchService.add(requestToBatchSuccessful); batchService.add(requestToBatchFailed); Future<Integer> result = batchService.flush(); batchService.stopAsync().awaitTerminated(); verify(retryPolicy).isRetryableStatusCode(httpErrorCode); assertEquals(Status.COMPLETED, requestToBatchSuccessful.getStatus()); assertEquals(Status.FAILED, requestToBatchFailed.getStatus()); assertEquals(1, requestToBatchSuccessful.getRetries()); assertEquals(0, requestToBatchFailed.getRetries()); assertEquals(successfulResult, requestToBatchSuccessful.getFuture().get()); validateFailedResult(requestToBatchFailed.getFuture()); assertFalse(batchService.isRunning()); assertEquals(result.get().intValue(), 1); }
Example 12
Source File: BatchRequestServiceTest.java From connector-sdk with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testRetryableGoogleJSONException() throws Exception { int httpErrorCode = 503; String errorMessage = "Service Unavailable"; when(retryPolicy.isRetryableStatusCode(httpErrorCode)).thenReturn(true); when(retryPolicy.getMaxRetryLimit()).thenReturn(3); 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(errorMessage); GoogleJsonResponseException exception = new GoogleJsonResponseException( new HttpResponseException.Builder(httpErrorCode, errorMessage, new HttpHeaders()), error); GenericJson successfulResult = new GenericJson(); doAnswer( i -> { if (requestToBatch.getRetries() >= 2) { requestToBatch.getCallback().onStart(); requestToBatch.getCallback().onSuccess(successfulResult, new HttpHeaders()); return null; } else { throw exception; } }) .when(batchRequestHelper) .executeBatchRequest(batchRequest); batchService.add(requestToBatch); batchService.flush(); batchService.stopAsync().awaitTerminated(); verify(retryPolicy, times(2)).isRetryableStatusCode(httpErrorCode); verify(retryPolicy, times(2)).getMaxRetryLimit(); verify(backOff, times(2)).nextBackOffMillis(); assertEquals(successfulResult, requestToBatch.getFuture().get()); assertEquals(Status.COMPLETED, requestToBatch.getStatus()); assertEquals(2, requestToBatch.getRetries()); assertFalse(batchService.isRunning()); }
Example 13
Source File: BatchRequestServiceTest.java From connector-sdk with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testFailedRequestSucceedsOnRetry() throws Exception { int httpErrorCode = 503; when(retryPolicy.isRetryableStatusCode(httpErrorCode)).thenReturn(true); when(retryPolicy.getMaxRetryLimit()).thenReturn(1); 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(httpErrorCode); error.setMessage("Service Unavailable"); GenericJson successfulResult = new GenericJson(); doAnswer( i -> { if (requestToBatch.getRetries() >= 1) { requestToBatch.getCallback().onStart(); requestToBatch.getCallback().onSuccess(successfulResult, new HttpHeaders()); } else { requestToBatch.getCallback().onFailure(error, new HttpHeaders()); } return null; }) .when(batchRequestHelper) .executeBatchRequest(batchRequest); batchService.add(requestToBatch); batchService.flush(); batchService.stopAsync().awaitTerminated(); verify(retryPolicy).isRetryableStatusCode(httpErrorCode); verify(retryPolicy).getMaxRetryLimit(); verify(backOff).nextBackOffMillis(); assertEquals(successfulResult, requestToBatch.getFuture().get()); assertEquals(Status.COMPLETED, requestToBatch.getStatus()); assertEquals(1, requestToBatch.getRetries()); assertFalse(batchService.isRunning()); }