亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在GWT的SERVER端添加異常處理程序?

如何在GWT的SERVER端添加異常處理程序?

瀟瀟雨雨 2021-04-09 18:19:46
我想包裝來自GWT服務器的所有傳出異常(僅服務器,客戶端上沒有任何內容):public class AuthServiceImpl extends RemoteServiceServlet implements AuthService {    public String loginAttemt(String username, String password) {        AwesomeUser user = AwesomeUserDao.forUsername(username);        if(BCrpyt.checkpw(user.getPasshash(), password)) {            String code = magicallyGenerateErrorCode();            logger.error("Invalid login attempt - error code"  + code);            throw new AwesomeException("password", "Invalid username or password", code);        }    }}@RemoteServiceRelativePath("auth")public interface AuthService extends RemoteService {    String loginAttempt(String username, String password) throws AwesomeException;}public interface AuthServiceAsync {    void loginAttempt(String username, String password, AsyncCallback<String> callback) throws AwesomeException;}現在,這可行。在我的客戶端上,我只是做這樣的事情(getField(String)出于不相關的原因我做了一些自定義,但這很重要)public void onFailure(Throwable caught) {    if(caught instanceof AwesomeException) {        AwesomeException a = (AwesomeException)caught;        getField(a.getfield()).setErrorText(a.getText());        getField("error-code").setText(a.getCode());    }}是的,這按預期工作。但是,想一想那user是null-你們中的一些人可能已經讀到“哦,伙計,如果用戶名不存在,他可能在那里有NPE”,您是絕對正確的。而且可以肯定的是,我確實在為這類事情編寫代碼。但是,我不會抓住所有這些情況。我想在某個地方添加一個異常處理程序,該異常處理程序捕獲所有這些異常并將其包裝在其中AwesomeException-特別是這樣,我可以將相關的錯誤代碼傳遞給用戶,以便他們可以與支持人員聯系以解決問題。我不想依靠程序員總是記住每個可能的null和非法參數以及所有其他運行時異常。我當然不希望他們自滿,(錯誤代碼實際上是一個請求ID-每個請求都有一個ID,并且所有日志消息在打印時都會使用該ID-因此,如果它們報告了錯誤代碼,我們可以非常輕松地查看帶有該請求的所有日志消息并解決錯誤)我嘗試跟蹤回調,但最終在RPC類中迷路了。但可以肯定的是,我不是第一個嘗試攔截服務器發出的GWT異常的人,對嗎?
查看完整描述

2 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

除了擴展服務之外RemoteServiceServlet,您還可以創建自己的servlet并委托給RemoteServiceServlet類似的東西:


public class GwtServiceServlet extends HttpServlet {

    public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {

        Object delegate = ...;


        RemoteServiceServlet remoteServiceServlet = new RemoteServiceServlet(delegate);

        remoteServiceServlet.init(getServletConfig());

        remoteServiceServlet.doPost(request, response);

    }

}

delegate服務接口的實現在哪里。由于您正在控制服務實現的創建,因此現在可以將其包裝在代理中以轉換異?;蜻M行日志記錄。


查看完整回答
反對 回復 2021-04-14
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

這可以通過擴展RemoteServiceServlet和在例如processCall(RPCRequest rpcRequest)或中進行異常處理來完成doUnexpectedFailure(Throwable e)。


例如:


僅針對不屬于服務方法簽名的異?;蝈e誤,或由SecurityException,SerializationExceptions或RPC框架內的其他故障導致的異常或錯誤調用此方法。


意味著這里可以將任何NPE等映射到自定義異常。


  protected void doUnexpectedFailure(Throwable e) {

    try {

      getThreadLocalResponse().reset();

    } catch (IllegalStateException ex) {

      /*

       * If we can't reset the request, the only way to signal that something

       * has gone wrong is to throw an exception from here. It should be the

       * case that we call the user's implementation code before emitting data

       * into the response, so the only time that gets tripped is if the object

       * serialization code blows up.

       */

      throw new RuntimeException("Unable to report failure", e);

    }

    ServletContext servletContext = getServletContext();


    String code = magicallyGenerateErrorCode();

    AwesomeException awesomeException = new AwesomeException("error", "Unexpected Error. Pls contact support", code);


    RPCServletUtils.writeResponseForUnexpectedFailure(servletContext,

        getThreadLocalResponse(), awesomeException);

  }


查看完整回答
反對 回復 2021-04-14
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號