HBASE-8987 ProtobufUtil.toException doesn't handle plain RemoteException, gets a NoSuchMethodException

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1504591 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2013-07-18 18:37:53 +00:00
parent 25e14c8b48
commit 20e0f2b625
2 changed files with 25 additions and 5 deletions

View File

@ -1204,17 +1204,22 @@ public final class ProtobufUtil {
* @throws IOException if failed to deserialize the parameter
*/
@SuppressWarnings("unchecked")
public static Throwable toException(
final NameBytesPair parameter) throws IOException {
public static Throwable toException(final NameBytesPair parameter) throws IOException {
if (parameter == null || !parameter.hasValue()) return null;
String desc = parameter.getValue().toStringUtf8();
String type = parameter.getName();
try {
Class<? extends Throwable> c =
(Class<? extends Throwable>)Class.forName(type, true, CLASS_LOADER);
Constructor<? extends Throwable> cn =
c.getDeclaredConstructor(String.class);
return cn.newInstance(desc);
Constructor<? extends Throwable> cn = null;
try {
cn = c.getDeclaredConstructor(String.class);
return cn.newInstance(desc);
} catch (NoSuchMethodException e) {
// Could be a raw RemoteException. See HBASE-8987.
cn = c.getDeclaredConstructor(String.class, String.class);
return cn.newInstance(type, desc);
}
} catch (Exception e) {
throw new IOException(e);
}

View File

@ -36,6 +36,7 @@ import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.Col
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue;
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.DeleteType;
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.MutationType;
import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameBytesPair;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@ -47,6 +48,20 @@ import com.google.protobuf.ByteString;
*/
@Category(SmallTests.class)
public class TestProtobufUtil {
@Test
public void testException() throws IOException {
NameBytesPair.Builder builder = NameBytesPair.newBuilder();
final String omg = "OMG!!!";
builder.setName("java.io.IOException");
builder.setValue(ByteString.copyFrom(Bytes.toBytes(omg)));
Throwable t = ProtobufUtil.toException(builder.build());
assertEquals(omg, t.getMessage());
builder.clear();
builder.setName("org.apache.hadoop.ipc.RemoteException");
builder.setValue(ByteString.copyFrom(Bytes.toBytes(omg)));
t = ProtobufUtil.toException(builder.build());
assertEquals(omg, t.getMessage());
}
/**
* Test basic Get conversions.