Search in sources :

Example 6 with BaseServiceException

use of com.google.cloud.BaseServiceException in project google-cloud-java by GoogleCloudPlatform.

the class TranslateExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    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);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    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);
    }
}
Also used : BaseServiceException(com.google.cloud.BaseServiceException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Example 7 with BaseServiceException

use of com.google.cloud.BaseServiceException in project google-cloud-java by GoogleCloudPlatform.

the class BigQueryExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    Exception cause = new BigQueryException(503, "message");
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        BigQueryException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(503, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertTrue(ex.isRetryable());
    } finally {
        verify(exceptionMock);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getMessage()).andReturn("message").times(1);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        BigQueryException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(BigQueryException.UNKNOWN_CODE, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertFalse(ex.isRetryable());
        assertSame(cause, ex.getCause());
    } finally {
        verify(exceptionMock);
    }
}
Also used : BaseServiceException(com.google.cloud.BaseServiceException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) HttpResponseException(com.google.api.client.http.HttpResponseException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Example 8 with BaseServiceException

use of com.google.cloud.BaseServiceException in project google-cloud-java by GoogleCloudPlatform.

the class BaseGrpcServiceExceptionTest method testTranslateAndThrow.

@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);
    }
}
Also used : RetryHelper(com.google.cloud.RetryHelper) SocketTimeoutException(java.net.SocketTimeoutException) BaseServiceException(com.google.cloud.BaseServiceException) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with BaseServiceException

use of com.google.cloud.BaseServiceException in project google-cloud-java by GoogleCloudPlatform.

the class BaseHttpServiceExceptionTest method testBaseServiceException.

@Test
public void testBaseServiceException() {
    BaseServiceException serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertNull(serviceException.getCause());
    serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertNull(serviceException.getCause());
    Exception cause = new RuntimeException();
    serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS, cause);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertEquals(cause, serviceException.getCause());
    serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, NOT_IDEMPOTENT, EMPTY_RETRYABLE_ERRORS, cause);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertEquals(cause, serviceException.getCause());
    IOException exception = new SocketTimeoutException();
    serviceException = new BaseHttpServiceException(exception, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertTrue(serviceException.isRetryable());
    assertNull(serviceException.getMessage());
    assertEquals(exception, serviceException.getCause());
    exception = new SocketException();
    serviceException = new BaseHttpServiceException(exception, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertTrue(serviceException.isRetryable());
    assertNull(serviceException.getMessage());
    assertEquals(exception, serviceException.getCause());
    exception = new IOException("insufficient data written");
    serviceException = new BaseHttpServiceException(exception, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertTrue(serviceException.isRetryable());
    assertEquals("insufficient data written", serviceException.getMessage());
    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());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertFalse(serviceException.isRetryable());
    serviceException = new CustomServiceException(CODE, MESSAGE, REASON, IDEMPOTENT);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertEquals(RETRYABLE, serviceException.isRetryable());
    serviceException = new CustomServiceException(CODE_NO_REASON, MESSAGE, null, IDEMPOTENT);
    assertEquals(CODE_NO_REASON, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertNull(serviceException.getReason());
    assertEquals(RETRYABLE, serviceException.isRetryable());
    serviceException = new CustomServiceException(UNKNOWN_CODE, MESSAGE, REASON, IDEMPOTENT);
    assertEquals(UNKNOWN_CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertEquals(RETRYABLE, serviceException.isRetryable());
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) BaseServiceException(com.google.cloud.BaseServiceException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) IOException(java.io.IOException) BaseServiceException(com.google.cloud.BaseServiceException) IOException(java.io.IOException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) Test(org.junit.Test)

Example 10 with BaseServiceException

use of com.google.cloud.BaseServiceException in project google-cloud-java by GoogleCloudPlatform.

the class BaseHttpServiceExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    BaseServiceException cause = new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    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);
    }
}
Also used : RetryHelper(com.google.cloud.RetryHelper) BaseServiceException(com.google.cloud.BaseServiceException) Test(org.junit.Test)

Aggregations

BaseServiceException (com.google.cloud.BaseServiceException)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 SocketTimeoutException (java.net.SocketTimeoutException)9 RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)6 RetryHelper (com.google.cloud.RetryHelper)3 HttpResponseException (com.google.api.client.http.HttpResponseException)2 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 SocketException (java.net.SocketException)1