ApiExceptionFactory { public static ApiException createException( Throwable cause, StatusCode statusCode, boolean retryable) { switch (statusCode.getCode()) { case CANCELLED: return new CancelledException(cause, statusCode, retryable); case NOT_FOUND: return new NotFoundException(cause, statusCode, retryable); case UNKNOWN: return new UnknownException(cause, statusCode, retryable); case INVALID_ARGUMENT: return new InvalidArgumentException(cause, statusCode, retryable); case DEADLINE_EXCEEDED: return new DeadlineExceededException(cause, statusCode, retryable); case ALREADY_EXISTS: return new AlreadyExistsException(cause, statusCode, retryable); case PERMISSION_DENIED: return new PermissionDeniedException(cause, statusCode, retryable); case RESOURCE_EXHAUSTED: return new ResourceExhaustedException(cause, statusCode, retryable); case FAILED_PRECONDITION: return new FailedPreconditionException(cause, statusCode, retryable); case ABORTED: return new AbortedException(cause, statusCode, retryable); case OUT_OF_RANGE: return new OutOfRangeException(cause, statusCode, retryable); case UNIMPLEMENTED: return new UnimplementedException(cause, statusCode, retryable); case INTERNAL: return new InternalException(cause, statusCode, retryable); case UNAVAILABLE: return new UnavailableException(cause, statusCode, retryable); case DATA_LOSS: return new DataLossException(cause, statusCode, retryable); case UNAUTHENTICATED: return new UnauthenticatedException(cause, statusCode, retryable); default: return new UnknownException(cause, statusCode, retryable); } } private  ApiExceptionFactory(); static ApiException createException(
      Throwable cause, StatusCode statusCode, boolean retryable); static ApiException createException(
      String message, Throwable cause, StatusCode statusCode, boolean retryable);  }
@Test public void invalidArgument() { Truth.assertThat(createException(Code.INVALID_ARGUMENT)) .isInstanceOf(InvalidArgumentException.class); Truth.assertThat(createExceptionWithMessage(Code.INVALID_ARGUMENT)) .isInstanceOf(InvalidArgumentException.class); }
@Test public void deadlineExceeded() { Truth.assertThat(createException(Code.DEADLINE_EXCEEDED)) .isInstanceOf(DeadlineExceededException.class); Truth.assertThat(createExceptionWithMessage(Code.DEADLINE_EXCEEDED)) .isInstanceOf(DeadlineExceededException.class); }
@Test public void alreadyExists() { Truth.assertThat(createException(Code.ALREADY_EXISTS)) .isInstanceOf(AlreadyExistsException.class); Truth.assertThat(createExceptionWithMessage(Code.ALREADY_EXISTS)) .isInstanceOf(AlreadyExistsException.class); }
@Test public void permissionDenied() { Truth.assertThat(createException(Code.PERMISSION_DENIED)) .isInstanceOf(PermissionDeniedException.class); Truth.assertThat(createExceptionWithMessage(Code.PERMISSION_DENIED)) .isInstanceOf(PermissionDeniedException.class); }
@Test public void resourceExhausted() { Truth.assertThat(createException(Code.RESOURCE_EXHAUSTED)) .isInstanceOf(ResourceExhaustedException.class); Truth.assertThat(createExceptionWithMessage(Code.RESOURCE_EXHAUSTED)) .isInstanceOf(ResourceExhaustedException.class); }
@Test public void failedPrecondition() { Truth.assertThat(createException(Code.FAILED_PRECONDITION)) .isInstanceOf(FailedPreconditionException.class); Truth.assertThat(createExceptionWithMessage(Code.FAILED_PRECONDITION)) .isInstanceOf(FailedPreconditionException.class); }
@Test public void aborted() { Truth.assertThat(createException(Code.ABORTED)).isInstanceOf(AbortedException.class); Truth.assertThat(createExceptionWithMessage(Code.ABORTED)).isInstanceOf(AbortedException.class); }
@Test public void outOfRange() { Truth.assertThat(createException(Code.OUT_OF_RANGE)).isInstanceOf(OutOfRangeException.class); Truth.assertThat(createExceptionWithMessage(Code.OUT_OF_RANGE)) .isInstanceOf(OutOfRangeException.class); }
@Test public void internal() { Truth.assertThat(createException(Code.INTERNAL)).isInstanceOf(InternalException.class); Truth.assertThat(createExceptionWithMessage(Code.INTERNAL)) .isInstanceOf(InternalException.class); }
@Test public void unavailable() { Truth.assertThat(createException(Code.UNAVAILABLE)).isInstanceOf(UnavailableException.class); Truth.assertThat(createExceptionWithMessage(Code.UNAVAILABLE)) .isInstanceOf(UnavailableException.class); }
@Test public void dataLoss() { Truth.assertThat(createException(Code.DATA_LOSS)).isInstanceOf(DataLossException.class); Truth.assertThat(createExceptionWithMessage(Code.DATA_LOSS)) .isInstanceOf(DataLossException.class); }
@Test public void unauthenticated() { Truth.assertThat(createException(Code.UNAUTHENTICATED)) .isInstanceOf(UnauthenticatedException.class); Truth.assertThat(createExceptionWithMessage(Code.UNAUTHENTICATED)) .isInstanceOf(UnauthenticatedException.class); }
@Test public void unimplemented() { Truth.assertThat(createException(Code.UNIMPLEMENTED)) .isInstanceOf(UnimplementedException.class); Truth.assertThat(createExceptionWithMessage(Code.UNIMPLEMENTED)) .isInstanceOf(UnimplementedException.class); }
@Test public void unknown_default() { Truth.assertThat(createException(Code.OK)).isInstanceOf(UnknownException.class); Truth.assertThat(createExceptionWithMessage(Code.OK)).isInstanceOf(UnknownException.class); }
@Test public void cancelled() { Truth.assertThat(createException(Code.CANCELLED)).isInstanceOf(CancelledException.class); Truth.assertThat(createExceptionWithMessage(Code.CANCELLED)) .isInstanceOf(CancelledException.class); }
@Test public void notFound() { Truth.assertThat(createException(Code.NOT_FOUND)).isInstanceOf(NotFoundException.class); Truth.assertThat(createExceptionWithMessage(Code.NOT_FOUND)) .isInstanceOf(NotFoundException.class); }
@Test public void unknown() { Truth.assertThat(createException(Code.UNKNOWN)).isInstanceOf(UnknownException.class); Truth.assertThat(createExceptionWithMessage(Code.UNKNOWN)).isInstanceOf(UnknownException.class); }