private static String message(IOException exception) { if (exception instanceof GoogleJsonResponseException) { GoogleJsonError details = ((GoogleJsonResponseException) exception).getDetails(); if (details != null) { return details.getMessage(); } } return exception.getMessage(); } }
@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; }
if (exception instanceof HttpResponseException) { if (exception instanceof GoogleJsonResponseException) { GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails(); if (jsonError != null) { BaseServiceException.Error error = new BaseServiceException.Error(jsonError.getCode(), reason(jsonError)); code = error.getCode(); reason = error.getReason(); retryable = error.isRetryable(idempotent, retryableErrors); if (reason != null) { GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0); location = errorInfo.getLocation(); debugInfo = (String) errorInfo.get("debugInfo"); code = ((GoogleJsonResponseException) exception).getStatusCode(); retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors);
private static ExceptionData makeExceptionData( GoogleJsonError googleJsonError, boolean idempotent, Set<BaseServiceException.Error> retryableErrors) { int code = googleJsonError.getCode(); String reason = reason(googleJsonError); ExceptionData.Builder exceptionData = ExceptionData.newBuilder(); exceptionData .setMessage(googleJsonError.getMessage()) .setCause(null) .setRetryable(BaseServiceException.isRetryable(code, reason, idempotent, retryableErrors)) .setCode(code) .setReason(reason); if (reason != null) { GoogleJsonError.ErrorInfo errorInfo = googleJsonError.getErrors().get(0); exceptionData.setLocation(errorInfo.getLocation()); exceptionData.setDebugInfo((String) errorInfo.get("debugInfo")); } else { exceptionData.setLocation(null); exceptionData.setDebugInfo(null); } return exceptionData.build(); }
assertSame(cause, exception.getCause()); GoogleJsonError error = new GoogleJsonError(); error.setCode(503); error.setMessage("message"); exception = new DnsException(error, true); assertEquals(503, exception.getCode());
@Test public void testGetChangeRequestNotFound() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<Change>> callback = Capture.newInstance(); Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance(); batchMock.addGetChangeRequest( EasyMock.eq(ZONE_NAME), EasyMock.eq(CHANGE_REQUEST_COMPLETE.getGeneratedId()), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); DnsBatchResult<ChangeRequest> batchResult = dnsBatch.getChangeRequest(ZONE_NAME, CHANGE_REQUEST_COMPLETE.getGeneratedId()); assertEquals(0, capturedOptions.getValue().size()); RpcBatch.Callback<Change> capturedCallback = callback.getValue(); GoogleJsonError error = new GoogleJsonError(); GoogleJsonError.ErrorInfo errorInfo = new GoogleJsonError.ErrorInfo(); errorInfo.setReason("reason"); errorInfo.setLocation("entity.parameters.changeId"); error.setCode(404); error.setErrors(ImmutableList.of(errorInfo)); capturedCallback.onFailure(error); assertNull(batchResult.get()); }
@Test public void testGetZoneNotFound() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<ManagedZone>> callback = Capture.newInstance(); Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance(); batchMock.addGetZone( EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); DnsBatchResult<Zone> batchResult = dnsBatch.getZone(ZONE_NAME); assertEquals(0, capturedOptions.getValue().size()); GoogleJsonError error = new GoogleJsonError(); error.setCode(404); RpcBatch.Callback<ManagedZone> capturedCallback = callback.getValue(); capturedCallback.onFailure(error); assertNull(batchResult.get()); }
@Test public void testDeleteAllArray() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<Void>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<Void>> callback2 = Capture.newInstance(); batchMock.addDelete( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addDelete( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Boolean> result = storage.delete(blobId1, blobId2); callback1.getValue().onSuccess(null); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, result.size()); assertTrue(result.get(0)); assertFalse(result.get(1)); EasyMock.verify(batchMock); }
if ( gjre.getDetails() != null && gjre.getDetails().getMessage() != null ) { exceptionToDisplay = new IOException( gjre.getDetails().getMessage(), gjre );
fileList = request.execute(); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); if (404 == error.getCode()) { throw new FileNotFoundException("File not found. Request: " + request);
assertEquals(exception, serviceException.getCause()); GoogleJsonError error = new GoogleJsonError(); error.setCode(CODE); error.setMessage(MESSAGE); serviceException = new BaseHttpServiceException(error, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS); assertEquals(CODE, serviceException.getCode());
GoogleJsonError error = new GoogleJsonError(); error.setCode(404); capturedCallback.onFailure(error); try {
@Test public void testDeleteAllIterable() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<Void>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<Void>> callback2 = Capture.newInstance(); batchMock.addDelete( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addDelete( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Boolean> result = storage.delete(blobId1, blobId2); callback1.getValue().onSuccess(null); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, result.size()); assertTrue(result.get(0)); assertFalse(result.get(1)); EasyMock.verify(batchMock); }
assertSame(cause, exception.getCause()); GoogleJsonError error = new GoogleJsonError(); error.setCode(503); error.setMessage("message"); exception = new StorageException(error); assertEquals(503, exception.getCode());
GoogleJsonError error = new GoogleJsonError(); error.setCode(404); capturedCallback.onFailure(error); try {
@Test public void testUpdateAllIterable() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addPatch( EasyMock.eq(BLOB_INFO1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addPatch( EasyMock.eq(BLOB_INFO2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.update(ImmutableList.of(BLOB_INFO1, BLOB_INFO2)); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }
@Test public void testUpdateAllArray() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addPatch( EasyMock.eq(BLOB_INFO1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addPatch( EasyMock.eq(BLOB_INFO2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.update(BLOB_INFO1, BLOB_INFO2); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }
@Test public void testGetAllArrayIterable() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addGet( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addGet( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.get(ImmutableList.of(blobId1, blobId2)); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }
@Test public void testGetAllArray() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addGet( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addGet( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.get(blobId1, blobId2); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }