代码之家  ›  专栏  ›  技术社区  ›  corsiKa

如何将异常处理程序添加到GWT的服务器端?

  •  0
  • corsiKa  · 技术社区  · 6 年前

    我想包装来自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

    (错误代码实际上是一个请求ID—每个请求都有一个ID,所有日志消息在打印时都使用该ID—因此,如果他们报告错误代码,我们可以很容易地看到包含该请求的所有日志消息并排除错误。)

    2 回复  |  直到 6 年前
        1
  •  1
  •   fgb    6 年前

    而不是服务扩展 RemoteServiceServlet ,您可以创建自己的servlet并委托给 远程服务Servlet 比如:

    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

        2
  •  1
  •   Tobika    6 年前

    RemoteServiceServlet 并执行异常处理,例如 processCall(RPCRequest rpcRequest) doUnexpectedFailure(Throwable e) .

    例如。:

    此方法仅对不属于该部分的异常或错误调用 服务方法的签名,或 SecurityExceptions、SerializationExceptions或

    意味着任何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);
      }