Java Code Examples for com.google.protobuf.ServiceException#getCause()

The following examples show how to use com.google.protobuf.ServiceException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestProtoBufRpc.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public static void testProtoBufRpc(TestRpcService client) throws Exception {  
  // Test ping method
  EmptyRequestProto emptyRequest = EmptyRequestProto.newBuilder().build();
  client.ping(null, emptyRequest);
  
  // Test echo method
  EchoRequestProto echoRequest = EchoRequestProto.newBuilder()
      .setMessage("hello").build();
  EchoResponseProto echoResponse = client.echo(null, echoRequest);
  Assert.assertEquals(echoResponse.getMessage(), "hello");
  
  // Test error method - error should be thrown as RemoteException
  try {
    client.error(null, emptyRequest);
    Assert.fail("Expected exception is not thrown");
  } catch (ServiceException e) {
    RemoteException re = (RemoteException)e.getCause();
    RpcServerException rse = (RpcServerException) re
        .unwrapRemoteException(RpcServerException.class);
    Assert.assertNotNull(rse);
    Assert.assertTrue(re.getErrorCode().equals(
        RpcErrorCodeProto.ERROR_RPC_SERVER));
  }
}
 
Example 2
Source File: TestProtoBufRpc.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Test (timeout=5000)
public void testProtoBufRandomException() throws Exception {
  TestRpcService client = getClient();
  EmptyRequestProto emptyRequest = EmptyRequestProto.newBuilder().build();

  try {
    client.error2(null, emptyRequest);
  } catch (ServiceException se) {
    Assert.assertTrue(se.getCause() instanceof RemoteException);
    RemoteException re = (RemoteException) se.getCause();
    Assert.assertTrue(re.getClassName().equals(
        URISyntaxException.class.getName()));
    Assert.assertTrue(re.getMessage().contains("testException"));
    Assert.assertTrue(
        re.getErrorCode().equals(RpcErrorCodeProto.ERROR_APPLICATION));
  }
}
 
Example 3
Source File: TestProtoBufRpc.java    From big-c with Apache License 2.0 6 votes vote down vote up
public static void testProtoBufRpc(TestRpcService client) throws Exception {  
  // Test ping method
  EmptyRequestProto emptyRequest = EmptyRequestProto.newBuilder().build();
  client.ping(null, emptyRequest);
  
  // Test echo method
  EchoRequestProto echoRequest = EchoRequestProto.newBuilder()
      .setMessage("hello").build();
  EchoResponseProto echoResponse = client.echo(null, echoRequest);
  Assert.assertEquals(echoResponse.getMessage(), "hello");
  
  // Test error method - error should be thrown as RemoteException
  try {
    client.error(null, emptyRequest);
    Assert.fail("Expected exception is not thrown");
  } catch (ServiceException e) {
    RemoteException re = (RemoteException)e.getCause();
    RpcServerException rse = (RpcServerException) re
        .unwrapRemoteException(RpcServerException.class);
    Assert.assertNotNull(rse);
    Assert.assertTrue(re.getErrorCode().equals(
        RpcErrorCodeProto.ERROR_RPC_SERVER));
  }
}
 
Example 4
Source File: TestProtoBufRpc.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Test (timeout=5000)
public void testProtoBufRandomException() throws Exception {
  TestRpcService client = getClient();
  EmptyRequestProto emptyRequest = EmptyRequestProto.newBuilder().build();

  try {
    client.error2(null, emptyRequest);
  } catch (ServiceException se) {
    Assert.assertTrue(se.getCause() instanceof RemoteException);
    RemoteException re = (RemoteException) se.getCause();
    Assert.assertTrue(re.getClassName().equals(
        URISyntaxException.class.getName()));
    Assert.assertTrue(re.getMessage().contains("testException"));
    Assert.assertTrue(
        re.getErrorCode().equals(RpcErrorCodeProto.ERROR_APPLICATION));
  }
}
 
Example 5
Source File: MRClientProtocolPBClientImpl.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private IOException unwrapAndThrowException(ServiceException se) {
  if (se.getCause() instanceof RemoteException) {
    return ((RemoteException) se.getCause()).unwrapRemoteException();
  } else if (se.getCause() instanceof IOException) {
    return (IOException)se.getCause();
  } else {
    throw new UndeclaredThrowableException(se.getCause());
  }
}
 
Example 6
Source File: ProtobufHelper.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Return the IOException thrown by the remote server wrapped in 
 * ServiceException as cause.
 * @param se ServiceException that wraps IO exception thrown by the server
 * @return Exception wrapped in ServiceException or
 *         a new IOException that wraps the unexpected ServiceException.
 */
public static IOException getRemoteException(ServiceException se) {
  Throwable e = se.getCause();
  if (e == null) {
    return new IOException(se);
  }
  return e instanceof IOException ? (IOException) e : new IOException(se);
}
 
Example 7
Source File: MRClientProtocolPBClientImpl.java    From big-c with Apache License 2.0 5 votes vote down vote up
private IOException unwrapAndThrowException(ServiceException se) {
  if (se.getCause() instanceof RemoteException) {
    return ((RemoteException) se.getCause()).unwrapRemoteException();
  } else if (se.getCause() instanceof IOException) {
    return (IOException)se.getCause();
  } else {
    throw new UndeclaredThrowableException(se.getCause());
  }
}
 
Example 8
Source File: ProtobufHelper.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Return the IOException thrown by the remote server wrapped in 
 * ServiceException as cause.
 * @param se ServiceException that wraps IO exception thrown by the server
 * @return Exception wrapped in ServiceException or
 *         a new IOException that wraps the unexpected ServiceException.
 */
public static IOException getRemoteException(ServiceException se) {
  Throwable e = se.getCause();
  if (e == null) {
    return new IOException(se);
  }
  return e instanceof IOException ? (IOException) e : new IOException(se);
}
 
Example 9
Source File: RPCUtil.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Utility method that unwraps and returns appropriate exceptions.
 * 
 * @param se
 *          ServiceException
 * @return An instance of the actual exception, which will be a subclass of
 *         {@link YarnException} or {@link IOException}
 */
public static Void unwrapAndThrowException(ServiceException se)
    throws IOException, YarnException {
  Throwable cause = se.getCause();
  if (cause == null) {
    // SE generated by the RPC layer itself.
    throw new IOException(se);
  } else {
    if (cause instanceof RemoteException) {
      RemoteException re = (RemoteException) cause;
      Class<?> realClass = null;
      try {
        realClass = Class.forName(re.getClassName());
      } catch (ClassNotFoundException cnf) {
        // Assume this to be a new exception type added to YARN. This isn't
        // absolutely correct since the RPC layer could add an exception as
        // well.
        throw instantiateException(YarnException.class, re);
      }

      if (YarnException.class.isAssignableFrom(realClass)) {
        throw instantiateException(
            realClass.asSubclass(YarnException.class), re);
      } else if (IOException.class.isAssignableFrom(realClass)) {
        throw instantiateException(realClass.asSubclass(IOException.class),
            re);
      } else if (RuntimeException.class.isAssignableFrom(realClass)) {
        throw instantiateException(
            realClass.asSubclass(RuntimeException.class), re);
      } else {
        throw re;
      }
      // RemoteException contains useful information as against the
      // java.lang.reflect exceptions.

    } else if (cause instanceof IOException) {
      // RPC Client exception.
      throw (IOException) cause;
    } else if (cause instanceof RuntimeException) {
      // RPC RuntimeException
      throw (RuntimeException) cause;
    } else {
      // Should not be generated.
      throw new IOException(se);
    }
  }
}
 
Example 10
Source File: RPCUtil.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Utility method that unwraps and returns appropriate exceptions.
 * 
 * @param se
 *          ServiceException
 * @return An instance of the actual exception, which will be a subclass of
 *         {@link YarnException} or {@link IOException}
 */
public static Void unwrapAndThrowException(ServiceException se)
    throws IOException, YarnException {
  Throwable cause = se.getCause();
  if (cause == null) {
    // SE generated by the RPC layer itself.
    throw new IOException(se);
  } else {
    if (cause instanceof RemoteException) {
      RemoteException re = (RemoteException) cause;
      Class<?> realClass = null;
      try {
        realClass = Class.forName(re.getClassName());
      } catch (ClassNotFoundException cnf) {
        // Assume this to be a new exception type added to YARN. This isn't
        // absolutely correct since the RPC layer could add an exception as
        // well.
        throw instantiateException(YarnException.class, re);
      }

      if (YarnException.class.isAssignableFrom(realClass)) {
        throw instantiateException(
            realClass.asSubclass(YarnException.class), re);
      } else if (IOException.class.isAssignableFrom(realClass)) {
        throw instantiateException(realClass.asSubclass(IOException.class),
            re);
      } else if (RuntimeException.class.isAssignableFrom(realClass)) {
        throw instantiateException(
            realClass.asSubclass(RuntimeException.class), re);
      } else {
        throw re;
      }
      // RemoteException contains useful information as against the
      // java.lang.reflect exceptions.

    } else if (cause instanceof IOException) {
      // RPC Client exception.
      throw (IOException) cause;
    } else if (cause instanceof RuntimeException) {
      // RPC RuntimeException
      throw (RuntimeException) cause;
    } else {
      // Should not be generated.
      throw new IOException(se);
    }
  }
}
 
Example 11
Source File: RPCUtil.java    From tez with Apache License 2.0 4 votes vote down vote up
/**
 * Utility method that unwraps and returns appropriate exceptions.
 *
 * @param se
 *          ServiceException
 * @return An instance of the actual exception, which will be a subclass of
 *         {@link TezException} or {@link IOException}
 */
public static Void unwrapAndThrowException(ServiceException se)
    throws IOException, TezException {

  Throwable cause = se.getCause();
  if (cause == null) {
    // SE generated by the RPC layer itself.
    throw new IOException(se);
  } else {
    if (cause instanceof RemoteException) {
      RemoteException re = (RemoteException) cause;
      Class<?> realClass = null;
      try {
        realClass = Class.forName(re.getClassName());
      } catch (ClassNotFoundException cnf) {
        // Assume this to be a new exception type added to Tez. This isn't
        // absolutely correct since the RPC layer could add an exception as
        // well.
        throw instantiateTezException(TezException.class, re);
      }

      if (SessionNotRunning.class.isAssignableFrom(realClass)) {
        throw instantiateTezException(
            realClass.asSubclass(SessionNotRunning.class), re);
      } else if (DAGNotRunningException.class.isAssignableFrom(realClass)) {
          throw instantiateTezException(
              realClass.asSubclass(DAGNotRunningException.class), re);
      } else if (TezException.class.isAssignableFrom(realClass)) {
        throw instantiateTezException(
            realClass.asSubclass(TezException.class), re);
      } else if (IOException.class.isAssignableFrom(realClass)) {
        throw instantiateIOException(realClass.asSubclass(IOException.class),
            re);
      } else if (RuntimeException.class.isAssignableFrom(realClass)) {
        throw instantiateRuntimeException(
            realClass.asSubclass(RuntimeException.class), re);
      } else {
        throw re;
      }
      // RemoteException contains useful information as against the
      // java.lang.reflect exceptions.

    } else if (cause instanceof IOException) {
      // RPC Client exception.
      throw (IOException) cause;
    } else if (cause instanceof RuntimeException) {
      // RPC RuntimeException
      throw (RuntimeException) cause;
    } else {
      // Should not be generated.
      throw new IOException(se);
    }
  }
}