| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -135,8 +135,21 @@ public BaseServiceException(IOException exception, boolean idempotent) { | |||
| 135 | 135 | this.debugInfo = debugInfo; | |
| 136 | 136 | } | |
| 137 | 137 | ||
| 138 | - public BaseServiceException(GoogleJsonError error, boolean idempotent) { | ||
| 139 | - this(error.getCode(), error.getMessage(), reason(error), idempotent); | ||
| 138 | + public BaseServiceException(GoogleJsonError googleJsonError, boolean idempotent) { | ||
| 139 | + super(googleJsonError.getMessage()); | ||
| 140 | + Error error = new Error(googleJsonError.getCode(), reason(googleJsonError)); | ||
| 141 | + this.code = error.code; | ||
| 142 | + this.reason = error.reason; | ||
| 143 | + this.retryable = isRetryable(idempotent, error); | ||
| 144 | + if (this.reason != null) { | ||
| 145 | + GoogleJsonError.ErrorInfo errorInfo = googleJsonError.getErrors().get(0); | ||
| 146 | + this.location = errorInfo.getLocation(); | ||
| 147 | + this.debugInfo = (String) errorInfo.get("debugInfo"); | ||
| 148 | + } else { | ||
| 149 | + this.location = null; | ||
| 150 | + this.debugInfo = null; | ||
| 151 | + } | ||
| 152 | + this.idempotent = idempotent; | ||
| 140 | 153 | } | |
| 141 | 154 | ||
| 142 | 155 | public BaseServiceException(int code, String message, String reason, boolean idempotent) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -91,7 +91,7 @@ public DnsBatchResult<Page<Zone>> listZones(Dns.ZoneListOption... options) { | |||
| 91 | 91 | public DnsBatchResult<Zone> createZone(ZoneInfo zone, Dns.ZoneOption... options) { | |
| 92 | 92 | DnsBatchResult<Zone> result = new DnsBatchResult<>(); | |
| 93 | 93 | // todo this can cause misleading report of a failure, intended to be fixed within #924 | |
| 94 | - RpcBatch.Callback<ManagedZone> callback = createZoneCallback(this.options, result, true); | ||
| 94 | + RpcBatch.Callback<ManagedZone> callback = createZoneCallback(this.options, result, false, true); | ||
| 95 | 95 | Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options); | |
| 96 | 96 | batch.addCreateZone(zone.toPb(), callback, optionMap); | |
| 97 | 97 | return result; | |
@@ -118,7 +118,7 @@ public DnsBatchResult<Boolean> deleteZone(String zoneName) { | |||
| 118 | 118 | */ | |
| 119 | 119 | public DnsBatchResult<Zone> getZone(String zoneName, Dns.ZoneOption... options) { | |
| 120 | 120 | DnsBatchResult<Zone> result = new DnsBatchResult<>(); | |
| 121 | - RpcBatch.Callback<ManagedZone> callback = createZoneCallback(this.options, result, true); | ||
| 121 | + RpcBatch.Callback<ManagedZone> callback = createZoneCallback(this.options, result, true, true); | ||
| 122 | 122 | Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options); | |
| 123 | 123 | batch.addGetZone(zoneName, callback, optionMap); | |
| 124 | 124 | return result; | |
@@ -186,7 +186,7 @@ public DnsBatchResult<Page<ChangeRequest>> listChangeRequests(String zoneName, | |||
| 186 | 186 | public DnsBatchResult<ChangeRequest> getChangeRequest(String zoneName, String changeRequestId, | |
| 187 | 187 | Dns.ChangeRequestOption... options) { | |
| 188 | 188 | DnsBatchResult<ChangeRequest> result = new DnsBatchResult<>(); | |
| 189 | - RpcBatch.Callback<Change> callback = createChangeRequestCallback(zoneName, result, true); | ||
| 189 | + RpcBatch.Callback<Change> callback = createChangeRequestCallback(zoneName, result, true, true); | ||
| 190 | 190 | Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options); | |
| 191 | 191 | batch.addGetChangeRequest(zoneName, changeRequestId, callback, optionMap); | |
| 192 | 192 | return result; | |
@@ -197,20 +197,21 @@ public DnsBatchResult<ChangeRequest> getChangeRequest(String zoneName, String ch | |||
| 197 | 197 | * {@code zoneName} to this batch. The {@code options} can be used to restrict the fields returned | |
| 198 | 198 | * in the same way as for {@link Dns#applyChangeRequest(String, ChangeRequestInfo, | |
| 199 | 199 | * Dns.ChangeRequestOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields | |
| 200 | - * the created {@link ChangeRequest} if successful, {@code null} if the change request does not | ||
| 201 | - * exist, or throws a {@link DnsException} if the operation failed or the zone does not exist. | ||
| 200 | + * the created {@link ChangeRequest} if successful or throws a {@link DnsException} if the | ||
| 201 | + * operation failed or the zone does not exist. | ||
| 202 | 202 | */ | |
| 203 | 203 | public DnsBatchResult<ChangeRequest> applyChangeRequest(String zoneName, | |
| 204 | 204 | ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) { | |
| 205 | 205 | DnsBatchResult<ChangeRequest> result = new DnsBatchResult<>(); | |
| 206 | - RpcBatch.Callback<Change> callback = createChangeRequestCallback(zoneName, result, false); | ||
| 206 | + RpcBatch.Callback<Change> callback = | ||
| 207 | + createChangeRequestCallback(zoneName, result, false, false); | ||
| 207 | 208 | Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options); | |
| 208 | 209 | batch.addApplyChangeRequest(zoneName, changeRequest.toPb(), callback, optionMap); | |
| 209 | 210 | return result; | |
| 210 | 211 | } | |
| 211 | 212 | ||
| 212 | 213 | /** | |
| 213 | - * Submits this batch for processing using a single HTTP request. | ||
| 214 | + * Submits this batch for processing using a single RPC request. | ||
| 214 | 215 | */ | |
| 215 | 216 | public void submit() { | |
| 216 | 217 | batch.submit(); | |
@@ -259,7 +260,7 @@ public void onFailure(GoogleJsonError googleJsonError) { | |||
| 259 | 260 | * A joint callback for both "get zone" and "create zone" operations. | |
| 260 | 261 | */ | |
| 261 | 262 | private RpcBatch.Callback<ManagedZone> createZoneCallback(final DnsOptions serviceOptions, | |
| 262 | - final DnsBatchResult<Zone> result, final boolean idempotent) { | ||
| 263 | + final DnsBatchResult<Zone> result, final boolean nullForNotFound, final boolean idempotent) { | ||
| 263 | 264 | return new RpcBatch.Callback<ManagedZone>() { | |
| 264 | 265 | @Override | |
| 265 | 266 | public void onSuccess(ManagedZone response) { | |
@@ -268,7 +269,12 @@ public void onSuccess(ManagedZone response) { | |||
| 268 | 269 | ||
| 269 | 270 | @Override | |
| 270 | 271 | public void onFailure(GoogleJsonError googleJsonError) { | |
| 271 | - result.error(new DnsException(googleJsonError, idempotent)); | ||
| 272 | + DnsException serviceException = new DnsException(googleJsonError, idempotent); | ||
| 273 | + if (nullForNotFound && serviceException.code() == HTTP_NOT_FOUND) { | ||
| 274 | + result.success(null); | ||
| 275 | + } else { | ||
| 276 | + result.error(serviceException); | ||
| 277 | + } | ||
| 272 | 278 | } | |
| 273 | 279 | }; | |
| 274 | 280 | } | |
@@ -337,17 +343,29 @@ public void onFailure(GoogleJsonError googleJsonError) { | |||
| 337 | 343 | * A joint callback for both "get change request" and "create change request" operations. | |
| 338 | 344 | */ | |
| 339 | 345 | private RpcBatch.Callback<Change> createChangeRequestCallback(final String zoneName, | |
| 340 | - final DnsBatchResult<ChangeRequest> result, final boolean idempotent) { | ||
| 346 | + final DnsBatchResult<ChangeRequest> result, final boolean nullForNotFound, | ||
| 347 | + final boolean idempotent) { | ||
| 341 | 348 | return new RpcBatch.Callback<Change>() { | |
| 342 | 349 | @Override | |
| 343 | 350 | public void onSuccess(Change response) { | |
| 344 | - result.success(response == null ? null : ChangeRequest.fromPb(options.service(), | ||
| 345 | - zoneName, response)); | ||
| 351 | + result.success(response == null ? null : ChangeRequest.fromPb(options.service(), zoneName, | ||
| 352 | + response)); | ||
| 346 | 353 | } | |
| 347 | 354 | ||
| 348 | 355 | @Override | |
| 349 | 356 | public void onFailure(GoogleJsonError googleJsonError) { | |
| 350 | - result.error(new DnsException(googleJsonError, idempotent)); | ||
| 357 | + DnsException serviceException = new DnsException(googleJsonError, idempotent); | ||
| 358 | + if (serviceException.code() == HTTP_NOT_FOUND) { | ||
| 359 | + if ("entity.parameters.changeId".equals(serviceException.location()) | ||
| 360 | + || (serviceException.getMessage() != null | ||
| 361 | + && serviceException.getMessage().contains("parameters.changeId"))) { | ||
| 362 | + // the change id was not found, but the zone exists | ||
| 363 | + result.success(null); | ||
| 364 | + return; | ||
| 365 | + } | ||
| 366 | + // the zone does not exist, so throw an exception | ||
| 367 | + } | ||
| 368 | + result.error(serviceException); | ||
| 351 | 369 | } | |
| 352 | 370 | }; | |
| 353 | 371 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,7 +110,7 @@ void addApplyChangeRequest(String zoneName, Change change, Callback<Change> call | |||
| 110 | 110 | Map<DnsRpc.Option, ?> options); | |
| 111 | 111 | ||
| 112 | 112 | /** | |
| 113 | - * Submits a batch of requests for processing using a single HTTP request to Cloud DNS. | ||
| 113 | + * Submits a batch of requests for processing using a single RPC request to Cloud DNS. | ||
| 114 | 114 | */ | |
| 115 | 115 | void submit(); | |
| 116 | 116 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,7 @@ | |||
| 19 | 19 | import static org.junit.Assert.assertEquals; | |
| 20 | 20 | import static org.junit.Assert.assertFalse; | |
| 21 | 21 | import static org.junit.Assert.assertNotNull; | |
| 22 | + import static org.junit.Assert.assertNull; | ||
| 22 | 23 | import static org.junit.Assert.assertSame; | |
| 23 | 24 | import static org.junit.Assert.assertTrue; | |
| 24 | 25 | import static org.junit.Assert.fail; | |
@@ -218,7 +219,9 @@ public void testCreateZone() { | |||
| 218 | 219 | } | |
| 219 | 220 | // testing error here, success is tested with options | |
| 220 | 221 | RpcBatch.Callback<ManagedZone> capturedCallback = callback.getValue(); | |
| 221 | - capturedCallback.onFailure(GOOGLE_JSON_ERROR); | ||
| 222 | + GoogleJsonError error = new GoogleJsonError(); | ||
| 223 | + error.setCode(404); | ||
| 224 | + capturedCallback.onFailure(error); | ||
| 222 | 225 | try { | |
| 223 | 226 | batchResult.get(); | |
| 224 | 227 | fail("Should throw a DnsException on error."); | |
@@ -279,6 +282,23 @@ public void testGetZone() { | |||
| 279 | 282 | } | |
| 280 | 283 | } | |
| 281 | 284 | ||
| 285 | + @Test | ||
| 286 | + public void testGetZoneNotFound() { | ||
| 287 | + EasyMock.reset(batchMock); | ||
| 288 | + Capture<RpcBatch.Callback<ManagedZone>> callback = Capture.newInstance(); | ||
| 289 | + Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance(); | ||
| 290 | + batchMock.addGetZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), | ||
| 291 | + EasyMock.capture(capturedOptions)); | ||
| 292 | + EasyMock.replay(batchMock); | ||
| 293 | + DnsBatchResult<Zone> batchResult = dnsBatch.getZone(ZONE_NAME); | ||
| 294 | + assertEquals(0, capturedOptions.getValue().size()); | ||
| 295 | + GoogleJsonError error = new GoogleJsonError(); | ||
| 296 | + error.setCode(404); | ||
| 297 | + RpcBatch.Callback<ManagedZone> capturedCallback = callback.getValue(); | ||
| 298 | + capturedCallback.onFailure(error); | ||
| 299 | + assertNull(batchResult.get()); | ||
| 300 | + } | ||
| 301 | + | ||
| 282 | 302 | @Test | |
| 283 | 303 | public void testGetZoneWithOptions() { | |
| 284 | 304 | EasyMock.reset(dns, batchMock, optionsMock); | |
@@ -556,6 +576,29 @@ public void testGetChangeRequest() { | |||
| 556 | 576 | } | |
| 557 | 577 | } | |
| 558 | 578 | ||
| 579 | + @Test | ||
| 580 | + public void testGetChangeRequestNotFound() { | ||
| 581 | + EasyMock.reset(batchMock); | ||
| 582 | + Capture<RpcBatch.Callback<Change>> callback = Capture.newInstance(); | ||
| 583 | + Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance(); | ||
| 584 | + batchMock.addGetChangeRequest(EasyMock.eq(ZONE_NAME), | ||
| 585 | + EasyMock.eq(CHANGE_REQUEST_COMPLETE.generatedId()), EasyMock.capture(callback), | ||
| 586 | + EasyMock.capture(capturedOptions)); | ||
| 587 | + EasyMock.replay(batchMock); | ||
| 588 | + DnsBatchResult<ChangeRequest> batchResult = dnsBatch.getChangeRequest(ZONE_NAME, | ||
| 589 | + CHANGE_REQUEST_COMPLETE.generatedId()); | ||
| 590 | + assertEquals(0, capturedOptions.getValue().size()); | ||
| 591 | + RpcBatch.Callback<Change> capturedCallback = callback.getValue(); | ||
| 592 | + GoogleJsonError error = new GoogleJsonError(); | ||
| 593 | + GoogleJsonError.ErrorInfo errorInfo = new GoogleJsonError.ErrorInfo(); | ||
| 594 | + errorInfo.setReason("reason"); | ||
| 595 | + errorInfo.setLocation("entity.parameters.changeId"); | ||
| 596 | + error.setCode(404); | ||
| 597 | + error.setErrors(ImmutableList.of(errorInfo)); | ||
| 598 | + capturedCallback.onFailure(error); | ||
| 599 | + assertNull(batchResult.get()); | ||
| 600 | + } | ||
| 601 | + | ||
| 559 | 602 | @Test | |
| 560 | 603 | public void testGetChangeRequestWithOptions() { | |
| 561 | 604 | EasyMock.reset(dns, batchMock, optionsMock); | |
@@ -599,7 +642,9 @@ public void testApplyChangeRequest() { | |||
| 599 | 642 | } | |
| 600 | 643 | // testing error here, success is tested with options | |
| 601 | 644 | RpcBatch.Callback<Change> capturedCallback = callback.getValue(); | |
| 602 | - capturedCallback.onFailure(GOOGLE_JSON_ERROR); | ||
| 645 | + GoogleJsonError error = new GoogleJsonError(); | ||
| 646 | + error.setCode(404); | ||
| 647 | + capturedCallback.onFailure(error); | ||
| 603 | 648 | try { | |
| 604 | 649 | batchResult.get(); | |
| 605 | 650 | fail("Should throw a DnsException on error."); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments