Thrift0.8當接口返回類型i16/i32/i64/bool時,客戶端無法捕獲服務端拋出的異常

測試代碼:

namespace java com.meituan.service

exception BugTestException{
    1: required i32 code;
    2: required string msg;
}

service HelloService {
    i32 bugCall() throws (1: BugTestException e);
}

實現類

@Service
public class HelloServiceImpl implements HelloService.Iface {
    @Override
    public int bugCall() throws BugTestException, TException {
        throw new BugTestException(100, "收不到就是bug");
    }
}

org.apache.thrift.ProcessFunction
在這裏插入圖片描述
當 getResult 執行出現異常

public static class bugCall_result implements org.apache.thrift.TBase<bugCall_result, bugCall_result._Fields>, java.io.Serializable, Cloneable {
    public int success; // required         
    public BugTestException e; // required  
}


@Override                                                                                            
protected bugCall_result getResult(I iface, bugCall_args args) throws org.apache.thrift.TException { 
    bugCall_result result = new bugCall_result();                                                    
    try {                                                                                            
        result.success = iface.bugCall();                                                            
        result.setSuccessIsSet(true);                                                                
    } catch (BugTestException e) {                                                                   
        result.e = e;                                                                                
    }                                                                                                
    return result;                                                                                   
}                                                                                                    

服務端也是返回 TMessageType.REPLY
result.write(oprot) --com.meituan.service.HelloService.bugCall_result.bugCall_resultStandardScheme#write

在這裏插入圖片描述
此時 struct.success 等於默認的 0,(Class 中基本類型默認值爲 0,引用類型默認值爲 null)

struct.isSetSuccess() 爲 false。

客戶端讀取操作:

com.meituan.service.HelloService.Client#recv_bugCall
在這裏插入圖片描述
org.apache.thrift.TServiceClient#receiveBase
在這裏插入圖片描述
此時讀取到的 msg.type == 2,即 TMessageType.REPLY。
com.meituan.service.HelloService.bugCall_result.bugCall_resultStandardScheme#read
在這裏插入圖片描述
struct.success 讀取到默認值 0,thrift 認爲讀取到了正確的值,所以執行 setSuccessIsSet。

故 com.meituan.service.HelloService.Client#recv_bugCall 直接返回了默認的 0,而沒有拋出異常。
在這裏插入圖片描述

解決 1

 public int recv_bugCall() throws BugTestException, org.apache.thrift.TException {
            bugCall_result result = new bugCall_result();
            receiveBase(result, "bugCall");
            // 先執行異常檢查
          	if (result.e != null) {
                throw result.e;
            }
          	if (result.isSetSuccess()) {
                return result.success;
            }
            throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "bugCall failed: unknown result");
        }

解決 2

將 getResult 用 try-catch 包裝起來。
這也是 thrift 0.9 版本的解決方案。

try {                                                                                       
  result = getResult(iface, args);                                                          
} catch(TException tex) {                                                                   
  LOGGER.error("Internal error processing " + getMethodName(), tex);                        
  TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR, 
    "Internal error processing " + getMethodName());                                        
  oprot.writeMessageBegin(new TMessage(getMethodName(), TMessageType.EXCEPTION, seqid));    
  x.write(oprot);                                                                           
  oprot.writeMessageEnd();                                                                  
  oprot.getTransport().flush();                                                             
  return;                                                                                   
}                   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章