@Test public void testBaseServiceException() { BaseServiceException serviceException = new BaseServiceException(ExceptionData.from(CODE, MESSAGE, REASON, NOT_RETRYABLE)); assertEquals(CODE, serviceException.getCode()); assertEquals(MESSAGE, serviceException.getMessage()); assertEquals(REASON, serviceException.getReason()); assertFalse(serviceException.isRetryable()); new BaseServiceException(ExceptionData.from(CODE, MESSAGE, REASON, NOT_RETRYABLE, cause)); assertEquals(CODE, serviceException.getCode()); assertEquals(MESSAGE, serviceException.getMessage()); new BaseServiceException( ExceptionData.newBuilder() .setMessage(MESSAGE) .setCause(cause) .setCode(CODE) .setReason(REASON) .setRetryable(RETRYABLE)
@Test public void testTranslateAndThrow() throws Exception { BaseServiceException cause = new BaseServiceException(ExceptionData.from(CODE, MESSAGE, REASON, NOT_RETRYABLE)); RetryHelper.RetryHelperException exceptionMock = createMock(RetryHelper.RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { BaseServiceException.translate(exceptionMock); } catch (BaseServiceException ex) { assertEquals(CODE, ex.getCode()); assertEquals(MESSAGE, ex.getMessage()); assertFalse(ex.isRetryable()); } finally { verify(exceptionMock); } }
private static ExceptionData makeExceptionData(IOException exception, boolean idempotent) { int code = UNKNOWN_CODE; Boolean retryable = null; if (exception instanceof HttpResponseException) { // In cases where an exception is an instance of HttpResponseException, // check the status code to determine whether it's retryable code = ((HttpResponseException) exception).getStatusCode(); retryable = BaseServiceException.isRetryable(code, null, idempotent, Collections.<Error>emptySet()); } return ExceptionData.newBuilder() .setMessage(exception.getMessage()) .setCause(exception) .setRetryable( MoreObjects.firstNonNull( retryable, BaseServiceException.isRetryable(idempotent, exception))) .setCode(code) .setReason(null) .setLocation(null) .setDebugInfo(null) .build(); }
@Test public void testNotifyError() { final BaseServiceException ex = new BaseServiceException(ExceptionData.from(0, "message", "reason", false)); assertFalse(result.completed()); BatchResult.Callback<Boolean, BaseServiceException> callback = EasyMock.createStrictMock(BatchResult.Callback.class); callback.error(ex); EasyMock.replay(callback); result.notify(callback); result.error(ex); try { result.notify(callback); fail("The batch has been completed."); } catch (IllegalStateException exception) { // expected } EasyMock.verify(callback); }
@InternalApi("This class should only be extended within google-cloud-java") protected BaseServiceException(ExceptionData exceptionData) { super(exceptionData.getMessage(), exceptionData.getCause()); this.code = exceptionData.getCode(); this.reason = exceptionData.getReason(); this.retryable = exceptionData.isRetryable(); this.location = exceptionData.getLocation(); this.debugInfo = exceptionData.getDebugInfo(); }
public ExceptionData build() { return new ExceptionData(message, cause, code, retryable, reason, location, debugInfo); } }
@InternalApi("This class should only be extended within google-cloud-java") protected BaseGrpcServiceException(String message, Throwable cause, int code, boolean retryable) { super( ExceptionData.newBuilder() .setMessage(message) .setCause(cause) .setCode(code) .setRetryable(retryable) .build()); }
public static ExceptionData from( int code, String message, String reason, boolean retryable, Throwable cause) { return newBuilder() .setCode(code) .setMessage(message) .setReason(reason) .setRetryable(retryable) .setCause(cause) .build(); }
public static ExceptionData from(int code, String message, String reason, boolean retryable) { return from(code, message, reason, retryable, null); }
@BetaApi public BaseGrpcServiceException(ApiException apiException) { super( ExceptionData.newBuilder() .setMessage(apiException.getMessage()) .setCause(apiException) .setRetryable(apiException.isRetryable()) .setCode(apiException.getStatusCode().getCode().getHttpStatusCode()) .setReason(apiException.getStatusCode().getCode().name()) .setLocation(null) .setDebugInfo(null) .build()); } }
public static ExceptionData from( int code, String message, String reason, boolean retryable, Throwable cause) { return newBuilder() .setCode(code) .setMessage(message) .setReason(reason) .setRetryable(retryable) .setCause(cause) .build(); }
public ExceptionData build() { return new ExceptionData(message, cause, code, retryable, reason, location, debugInfo); } }
@InternalApi("This class should only be extended within google-cloud-java") protected BaseServiceException(ExceptionData exceptionData) { super(exceptionData.getMessage(), exceptionData.getCause()); this.code = exceptionData.getCode(); this.reason = exceptionData.getReason(); this.retryable = exceptionData.isRetryable(); this.location = exceptionData.getLocation(); this.debugInfo = exceptionData.getDebugInfo(); }
@InternalApi("This class should only be extended within google-cloud-java") protected BaseGrpcServiceException(String message, Throwable cause, int code, boolean retryable) { super( ExceptionData.newBuilder() .setMessage(message) .setCause(cause) .setCode(code) .setRetryable(retryable) .build()); }
@BetaApi public BaseGrpcServiceException(ApiException apiException) { super( ExceptionData.newBuilder() .setMessage(apiException.getMessage()) .setCause(apiException) .setRetryable(apiException.isRetryable()) .setCode(apiException.getStatusCode().getCode().getHttpStatusCode()) .setReason(apiException.getStatusCode().getCode().name()) .setLocation(null) .setDebugInfo(null) .build()); } }
public static ExceptionData from(int code, String message, String reason, boolean retryable) { return from(code, message, reason, retryable, null); }
public CustomServiceException(int code, String message, String reason, boolean idempotent) { super( ExceptionData.from( code, message, reason, BaseServiceException.isRetryable(code, reason, idempotent, RETRYABLE_ERRORS))); }
@Test public void testError() { assertFalse(result.completed()); try { result.get(); fail("This was not completed yet."); } catch (IllegalStateException ex) { // expected } try { result.error(null); fail(); } catch (NullPointerException exc) { // expected } BaseServiceException ex = new BaseServiceException(ExceptionData.from(0, "message", "reason", false)); result.error(ex); try { result.get(); fail("This is a failed operation and should have thrown a DnsException."); } catch (BaseServiceException real) { assertSame(ex, real); } }
retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors); retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors); return ExceptionData.newBuilder() .setMessage(message(exception)) .setCause(exception) .setRetryable( MoreObjects.firstNonNull( retryable, BaseServiceException.isRetryable(idempotent, exception))) .setCode(code) .setReason(reason)
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(); }