@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); } }
@Override public int hashCode() { return Objects.hash(getCause(), getMessage(), code, retryable, reason, location, debugInfo); } }
Exception cause = new DnsException(timeoutException, true); RetryHelperException exceptionMock = createMock(RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { DnsException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(DnsException.UNKNOWN_CODE, ex.getCode()); assertNull(ex.getReason()); assertEquals("message", ex.getMessage()); assertEquals(timeoutException, ex.getCause()); assertTrue(ex.isRetryable()); } finally { verify(exceptionMock); expect(exceptionMock.getMessage()).andReturn("message").times(1); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { DnsException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(DnsException.UNKNOWN_CODE, ex.getCode()); assertEquals("message", ex.getMessage()); assertFalse(ex.isRetryable()); assertSame(cause, ex.getCause()); } finally { verify(exceptionMock);
@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()); assertNull(serviceException.getCause()); new BaseServiceException(ExceptionData.from(CODE, MESSAGE, REASON, NOT_RETRYABLE, cause)); assertEquals(CODE, serviceException.getCode()); assertEquals(MESSAGE, serviceException.getMessage()); assertEquals(REASON, serviceException.getReason()); assertFalse(serviceException.isRetryable()); assertEquals(cause, serviceException.getCause()); new BaseServiceException( ExceptionData.newBuilder() .setMessage(MESSAGE) .setCause(cause) .setLocation(LOCATION) .build()); assertEquals(CODE, serviceException.getCode()); assertEquals(MESSAGE, serviceException.getMessage()); assertEquals(REASON, serviceException.getReason()); assertTrue(serviceException.isRetryable()); assertEquals(cause, serviceException.getCause()); assertEquals(DEBUG_INFO, serviceException.getDebugInfo());
@Override public RetryResult beforeEval(Exception exception) { if (exception instanceof BaseServiceException) { boolean retriable = ((BaseServiceException) exception).isRetryable(); return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.CONTINUE_EVALUATION; } return Interceptor.RetryResult.CONTINUE_EVALUATION; } };
static BaseServiceException translateAndThrow(ExecutionException ex) { BaseServiceException.translate(ex); throw new ComputeException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } }
public BaseServiceException(IOException exception, boolean idempotent) { super(message(exception), exception); int code = UNKNOWN_CODE; String reason = null; String location = null; String debugInfo = null; Boolean retryable = null; if (exception instanceof GoogleJsonResponseException) { GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails(); if (jsonError != null) { Error error = new Error(jsonError.getCode(), reason(jsonError)); code = error.code; reason = error.reason; retryable = isRetryable(idempotent, error); if (reason != null) { GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0); location = errorInfo.getLocation(); debugInfo = (String) errorInfo.get("debugInfo"); } } else { code = ((GoogleJsonResponseException) exception).getStatusCode(); } } this.retryable = MoreObjects.firstNonNull(retryable, isRetryable(idempotent, exception)); this.code = code; this.reason = reason; this.idempotent = idempotent; this.location = location; this.debugInfo = debugInfo; }
public BaseServiceException(GoogleJsonError googleJsonError, boolean idempotent) { super(googleJsonError.getMessage()); Error error = new Error(googleJsonError.getCode(), reason(googleJsonError)); this.code = error.code; this.reason = error.reason; this.retryable = isRetryable(idempotent, error); if (this.reason != null) { GoogleJsonError.ErrorInfo errorInfo = googleJsonError.getErrors().get(0); this.location = errorInfo.getLocation(); this.debugInfo = (String) errorInfo.get("debugInfo"); } else { this.location = null; this.debugInfo = null; } this.idempotent = idempotent; }
/** * Translate RetryHelperException to the BigQueryException that caused the error. This method will * always throw an exception. * * @throws BigQueryException when {@code ex} was caused by a {@code BigQueryException} * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static BaseServiceException translateAndThrow(RetryHelperException ex) { BaseServiceException.translateAndPropagateIfPossible(ex); throw new BigQueryException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } }
Exception cause = new ComputeException(500, "message"); RetryHelperException exceptionMock = createMock(RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { ComputeException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(500, ex.getCode()); assertEquals("message", ex.getMessage()); assertTrue(ex.isRetryable()); } finally { verify(exceptionMock); expect(exceptionMock.getMessage()).andReturn("message").times(1); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { ComputeException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(ComputeException.UNKNOWN_CODE, ex.getCode()); assertEquals("message", ex.getMessage()); assertFalse(ex.isRetryable()); assertSame(cause, ex.getCause()); } finally { verify(exceptionMock);
@InternalApi public boolean isRetryable(boolean idempotent, Set<Error> retryableErrors) { return BaseServiceException.isRetryable(code, reason, idempotent, retryableErrors); }
static BaseServiceException translateAndThrow(ExecutionException ex) { BaseServiceException.translate(ex); throw new BigQueryException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } }
/** * Translate RetryHelperException to the DnsException that caused the error. This method will * always throw an exception. * * @throws DnsException when {@code ex} was caused by a {@code DnsException} * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static DnsException translateAndThrow(RetryHelperException ex) { BaseServiceException.translateAndPropagateIfPossible(ex); throw new DnsException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } }
Exception cause = new StorageException(503, "message"); RetryHelperException exceptionMock = createMock(RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { StorageException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(503, ex.getCode()); assertEquals("message", ex.getMessage()); assertTrue(ex.isRetryable()); } finally { verify(exceptionMock); expect(exceptionMock.getMessage()).andReturn("message").times(1); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { StorageException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(StorageException.UNKNOWN_CODE, ex.getCode()); assertEquals("message", ex.getMessage()); assertFalse(ex.isRetryable()); assertSame(cause, ex.getCause()); } finally { verify(exceptionMock);
@Test public void testTranslateAndThrow() throws Exception { IOException exception = new SocketTimeoutException(); BaseGrpcServiceException cause = new BaseGrpcServiceException(exception, IDEMPOTENT); RetryHelper.RetryHelperException exceptionMock = createMock(RetryHelper.RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { BaseServiceException.translate(exceptionMock); } catch (BaseServiceException ex) { assertEquals(0, ex.getCode()); assertNull(ex.getMessage()); assertTrue(ex.isRetryable()); } finally { verify(exceptionMock); } } }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof BaseServiceException)) { return false; } BaseServiceException other = (BaseServiceException) obj; return Objects.equals(getCause(), other.getCause()) && Objects.equals(getMessage(), other.getMessage()) && code == other.code && retryable == other.retryable && Objects.equals(reason, other.reason) && Objects.equals(location, other.location) && Objects.equals(debugInfo, other.debugInfo); }
public CustomServiceException(int code, String message, String reason, boolean idempotent) { super( ExceptionData.from( code, message, reason, BaseServiceException.isRetryable(code, reason, idempotent, RETRYABLE_ERRORS))); }
/** * Translate RetryHelperException to the ComputeException that caused the error. This method will * always throw an exception. * * @throws ComputeException when {@code ex} was caused by a {@code ComputeException} */ static BaseServiceException translateAndThrow(RetryHelperException ex) { BaseServiceException.translate(ex); throw new ComputeException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); }
/** * Translate RetryHelperException to the TranslateException that caused the error. This method * will always throw an exception. * * @throws TranslateException when {@code ex} was caused by a {@code TranslateException} * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static BaseServiceException translateAndThrow(RetryHelperException ex) { BaseServiceException.translateAndPropagateIfPossible(ex); throw new TranslateException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } }
Exception cause = new TranslateException(500, "message"); RetryHelperException exceptionMock = createMock(RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { TranslateException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(500, ex.getCode()); assertEquals("message", ex.getMessage()); assertTrue(ex.isRetryable()); } finally { verify(exceptionMock); expect(exceptionMock.getMessage()).andReturn("message").times(1); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { TranslateException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(TranslateException.UNKNOWN_CODE, ex.getCode()); assertEquals("message", ex.getMessage()); assertFalse(ex.isRetryable()); assertSame(cause, ex.getCause()); } finally { verify(exceptionMock);