Search in sources :

Example 1 with BaseServiceException

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

the class ComputeExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    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);
    }
    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 {
        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);
    }
}
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 2 with BaseServiceException

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

the class ResourceManagerExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    Exception cause = new ResourceManagerException(503, "message");
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        ResourceManagerException.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 {
        ResourceManagerException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(ResourceManagerException.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 3 with BaseServiceException

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

the class DatastoreExceptionTest method testTranslateAndThrow.

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

Example 4 with BaseServiceException

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

the class DnsExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    IOException timeoutException = new SocketTimeoutException("message");
    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);
    }
    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 {
        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);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) BaseServiceException(com.google.cloud.BaseServiceException) IOException(java.io.IOException) 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 5 with BaseServiceException

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

the class StorageExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    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);
    }
    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 {
        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);
    }
}
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)

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