1

私は試した

public void onFailure(Throwable caught) {
    Throwable cause = caught.getCause();
    String causeStr = (cause==null) ? "" : ", "+cause.getMessage();
    errorLabel.setText(SERVER_ERROR + ": " + caught.getMessage() + causeStr);

しかし、原因は常に null であり、caught.getMessage()常に非常に一般的な500 The call failed on the server; see server log for details. サーバーから IllegalArgumentExceptions をスローし、それをクライアントに表示できるようにしたい:

throw new IllegalArgumentException("Email address is invalid.");
4

5 に答える 5

2

カスタム例外ハンドラをオーバーライドしRequestFactoryServletて渡すこともできます::

public class CustomRequestFactoryServlet extends RequestFactoryServlet {

    private static class ApplicationExceptionLogger implements ExceptionHandler {

        private final Logger log = LoggerFactory.getLogger(ApplicationExceptionLogger.class);

        @Override
        public ServerFailure createServerFailure(Throwable throwable) {
            log.error("Server Error", throwable);
            return new ServerFailure(throwable.getMessage(), throwable.getClass().getName(), throwable.getStackTrace().toString(), true);
        }
    }

    public CustomRequestFactoryServlet() {
       super(new ApplicationExceptionLogger());
    }
}

web.xml 内 ::

<servlet>
    <servlet-name>requestFactoryServlet</servlet-name>
    <servlet-class>com.myvdm.server.CustomRequestFactoryServlet</servlet-class>
</servlet>
于 2013-10-11T17:54:38.540 に答える
1

また、Google UmbrellaException を送り返すことができることもわかりましたが、コンストラクターで Sets しか取得しないため、少しおかしなことにインスタンス化する必要があります。

サーバ

public String getUserId () throws Exception {
    Set<Throwable> s = new HashSet<Throwable>(Arrays.asList(new IllegalArgumentException("Hidey hidey ho!")));
    if (true) throw new com.google.gwt.event.shared.UmbrellaException(s);

クライアント

        public void onFailure(Throwable caught) {
            log.severe("fetchUserName(), Could not fetch username: " + caught.getMessage());

コンソール

Mon Oct 14 12:05:28 EDT 2013 com.example.client.Login
SEVERE: fetchUserName(), Could not fetch username: Exception caught: Hidey hidey ho!
于 2013-10-14T16:18:03.410 に答える
0

Zied と Fred の回答は、最もシンプルで透過的であるため、最も気に入りました。ただし、UncaughtExceptionHandler を使用したり、SystemExceptions を作成したりする必要がないため、さらに簡単になります。例外を通常どおりにキャプチャし、再ラップしてスローするだけです。サーバーインターフェースに例外を散らかす必要はありません(あなたのものだけ)。OutOfMemoryError のような重大なエラーは、GWT によって通常どおり処理されます。また、私の他の答えよりもインスタンス化が簡単です。GWT には既にパス/フェイル ハンドラがあるため、特別な戻り値でonSuccess/onFailureエラーを再チェックする必要はありません。onSuccessただし、到達する唯一の方法onFailureは Exception を使用することです。そのため、ブール値で十分かもしれませんが、クライアント ハンドラーにエラーを示すには Exception が必要です。

私の例外

package com.example.shared;

import java.io.Serializable;

public class MyException extends Exception implements Serializable {
    private static final long serialVersionUID = 1104312904865934899L;

    public MyException() {}

    public MyException (String s) {
        super(s);
    }
}

サーバ

public void cancelSend() throws MyException {
    throw new MyException("Because I said so");
于 2013-10-14T21:13:08.597 に答える