1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  package org.apache.hadoop.hbase.procedure2;
19  
20  import java.io.IOException;
21  
22  import org.apache.hadoop.ipc.RemoteException;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.protobuf.generated.ErrorHandlingProtos.ForeignExceptionMessage;
26  import org.apache.hadoop.hbase.util.ForeignExceptionUtil;
27  
28  import com.google.protobuf.InvalidProtocolBufferException;
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  @InterfaceAudience.Private
42  @InterfaceStability.Evolving
43  @SuppressWarnings("serial")
44  public class RemoteProcedureException extends ProcedureException {
45  
46    
47  
48  
49    private final String source;
50  
51    
52  
53  
54  
55  
56  
57    public RemoteProcedureException(String source, Throwable cause) {
58      super(cause);
59      assert source != null;
60      assert cause != null;
61      this.source = source;
62    }
63  
64    public String getSource() {
65      return source;
66    }
67  
68    public IOException unwrapRemoteException() {
69      if (getCause() instanceof RemoteException) {
70        return ((RemoteException)getCause()).unwrapRemoteException();
71      }
72      if (getCause() instanceof IOException) {
73        return (IOException)getCause();
74      }
75      return new IOException(getCause());
76    }
77  
78    @Override
79    public String toString() {
80      String className = getCause().getClass().getName();
81      return className + " via " + getSource() + ":" + getLocalizedMessage();
82    }
83  
84    
85  
86  
87  
88  
89  
90    public static byte[] serialize(String source, Throwable t) {
91      return toProto(source, t).toByteArray();
92    }
93  
94    
95  
96  
97  
98  
99  
100   public static RemoteProcedureException deserialize(byte[] bytes)
101       throws InvalidProtocolBufferException {
102     return fromProto(ForeignExceptionMessage.parseFrom(bytes));
103   }
104 
105   public ForeignExceptionMessage convert() {
106     return ForeignExceptionUtil.toProtoForeignException(getSource(), getCause());
107   }
108 
109   public static ForeignExceptionMessage toProto(String source, Throwable t) {
110     return ForeignExceptionUtil.toProtoForeignException(source, t);
111   }
112 
113   public static RemoteProcedureException fromProto(final ForeignExceptionMessage eem) {
114     return new RemoteProcedureException(eem.getSource(), ForeignExceptionUtil.toIOException(eem));
115   }
116 }